编程练习5/15

来源:互联网 发布:卓智网络 锐捷 编辑:程序博客网 时间:2024/06/04 23:31

一、字符串截断输出
首先输入两个整数m,n,分别代表行数和长度,要求输出,将字符串截短成n长,不够补0,多了换行继续输出
如输入:
2,8
1234567812345678123
asd

输出
12345678
12345678
12300000
asd00000

#include <iostream>#include <string>using namespace std;//首先输入两个整数m,n,分别代表行数和长度,要求输出,将字符串截短成n长,不够补0,多了换行继续输出int main(){    int max = 30; //控制输入字符串的最大长度    int m, n;    printf("输入字符串行数m、输出的字符长度n,(m>0, n>0)\n");    scanf("%d %d",&m, &n);    string s;    string *p = new string[m];    cout<<"输入每行的字符:\n";    for (int i = 0; i < m; i++)    {        cin >> s;        p[i] = s;    }    cout<<"按长度为" << n << "输出字符串\n";    for (int i = 0; i < m; i++)    {        int len = p[i].size();        for (int j = 0; j < len; j++)        {            if (j != 0 && j%n == 0)            {                cout << endl;            }            cout << p[i][j];         }        int k = len % n;        if(k != 0)        {            for (int l = 0; l < (n-k); l++)            cout<<"0";        }        cout<<endl;    }    system("pause");    return 0;}

二、字符串过滤后排序
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉,然后按字母表顺序输出。
比如字符串“abfcacde”输出结果为“abcdef”。

#include <iostream>#include <string>using namespace std;//通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉,然后按字母表顺序输出。//比如字符串“abfcacde”输出结果为“abcdef”。int main(){    int a[26] = {0};    string s;    cout << "输入小写字符串(a~z):\n";    cin >> s;    int len = s.size();    for (int i = 0; i < len; i++)    {        a[s[i]-'a'] ++;    }    cout << "输出字符串为:\n";    for(int i = 0; i < 26; i++)    {        if(a[i] >= 1)        {             printf("%c", 'a' + i);        }    }    cout << endl;    system("pause");    return 0;       }

三、通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
压缩规则:
1. 仅压缩连续重复出现的字符。比如字符串”abcbc”由于无连续重复字符,压缩后的字符串还是”abcbc”.
2. 压缩字段的格式为”字符重复的次数+字符”。例如:字符串”xxxyyyyyyz”压缩后就成为”3x6yz”

//以子函数实现:#include <iostream>#include <string>using namespace std;void stringZip( char *instring, int len, char *outstring){    int count = 1;    for (int i = 0; i < len; i++)    {        if(instring[i+1] == instring[i])            count++;        else        {            if(count == 1)                *outstring++ = instring[i];            else            {                *outstring++ = count + '0'; //把数字转换成字符                *outstring++ = instring[i];            }        }    }   }int main(){    char *instr = {"aabbbcdeffg"};    int len = strlen(instr);    char *outstr = (char *) malloc(len *sizeof(char));    stringZip(instr, len, outstr);    cout<<outstr<<endl;    system("pause");    return 0;}

在main()函数实现:

int main(){    string s;    cout << "输入一串小写字母(a~z)组成的字符串\n";    cin >> s;    int len = s.size();    int count = 1;    for (int i = 0; i < len ; i++)    {        if(s[i+1]-s[i] == 0)            count ++;        else         {            if(count ==1)                printf("%c", s[i]);            else                 printf("%d%c",count, s[i]);            count = 1;        }    }    system("pause");    return 0;}
0 0
原创粉丝点击