美文网首页
字符串集锦

字符串集锦

作者: nino天 | 来源:发表于2014-08-21 13:18 被阅读41次

1.统计字符数问题:首先遍历单词,用一个数组去记录每个字符出现的次数,其次遍历sum数组找到单词出现的最大值,最后输出要进行类型转换...

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int i,nCase,max;
    string word;
    int sum[26]={0};
    cin>>nCase;
    while(nCase--)
    {
        cin>>word;
        for(i=0;i<word.length();i++)//遍历单词
            sum[word[i]-'a']++;
        max = 0;
        for(i=0;i<26;i++)//遍历数组
            if(sum[i]>sum[max]) max=i;
        cout<<(char)(max+'a') <<sum[max]<<endl;
    }
    return 0;
}

2.电话簿问题,找出相同的号码并计算出现的次数。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char map[]="22233344455566677778889999";
char str[80],tel[100000][9];

int compare(const void *elem1,const void *elem2)
{
    return strcmp((char*)elem1,(char*)elem2);
}


void stdTel(int i)
{
    int j=-1,k=-1;
    while(k<8)
    {
        j++;
        if(str[j]=='-')
            continue;
        k++;
        if(k==3){
           tel[i][k]='-';
           k++;
        }
        if(str[j]>='A' && str[j]<='Z')
            tel[i][k]=map[str[j]-'A'];
        else
            tel[i][k]=str[j];
    }
    tel[i][k]='\0';
    return;
}

void main()
{
    int n,i,j;
    bool Flag;
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
        scanf("%s",str);
        stdTel(i);
    }
    qsort(tel,n,9,compare);

    Flag = true;
    i = 0;
    while(i<n)
    {
        j=i;
        i++;
        while(i<n&&strcmp(tel[i],tel[j])==0) i++;
        if(i-j>1){
           printf("%s %d\n",tel[j],i-j);
           Flag = false;
        }
    }
    if(Flag)
        printf("No duplicates.\n");
}

3.寻找子串问题
输入n个字符串,找到一个最长的字符串x,使得对已经给出的n个字符串中的任意一个y,x是y的子串,或者反序x是y的子串。

注意几个函数的用法:strlen(字符串长度),strncpy(子串复制),strcpy(字符串复制),strstr(子串匹配),strrev(字符串反序)

#include <stdio.h>
#include <string.h>
char str[100][101];
int n;

int searchMaxSubString(char* source)//搜索其所有子串是否满足条件
{
   int subStrLen = strlen(source),sourceStrLen = strlen(source);
   int i=0,j,Found;
   char subStr[101],revsubStr[101];//用来存当前搜索的字符串
   while(subStrLen>0)
   {
       Found = true;
       strncpy(subStr,source+i,subStrLen);//复制字符串的子串
                                          //指定子串起始位置和子串长度
       strncpy(revsubStr,source+i,subStrLen);
       subStr[subStrLen]=revsubStr[subStrLen]='\0';
       strrev(revsubStr);//对字符串进行反序
 
       for(j=0;j<n;j++)
       if(strstr(str[j],subStr)==NULL && strstr(str[j],revsubStr)==NULL)
       {
           Found = false;
           break;
       }
       
       if(Found) return subStrLen;
       subStrLen--;
       i++;
   }
   return 0;
}
void main()
{
    int t;
    int i,minStrLen,subStrLen;
    char minStr[101];

    scanf("%d\n",&t);
    while(t--)
    {
        scanf("%d",&n);
        minStrLen = 100;
        for(i=0;i<n;i++)
        {
            scanf("%s",str[i]);
            if(strlen(str[i])<minStrLen)
            {
                strcpy(minStr,str[i]);
                minStrLen = strlen(minStr);
            }
        }
        subStrLen = searchMaxSubString(minStr);
        printf("%d\n",subStrLen);
    }
}

4.CAESAR密码问题:注意输入输出用的gets,以及结束输入的用法

#include <stdio.h>
#include <string.h>

void decipher(char message[])
{
    char plain[27] = "VWXYZABCDEFGHIJKLMNOPQRSTU";
    char cipherEnd[201];
    int i;

    gets(message);
    int cipherLen = strlen(message);
    for(i=0;i<cipherLen;i++)
        if(message[i]>='A' && message[i]<='Z')
            message[i]=plain[message[i]-'A'];

    gets(cipherEnd);
    return;
}
void main()
{
    char message[201];
    gets(message);
    while(strcmp(message,"START")==0)
    {
        decipher(message);
        printf("%s\n",message);
        gets(message);
        if(strcmp(message,"ENDOFINPUT")==0)
            break;
    }
}

相关文章

网友评论

      本文标题:字符串集锦

      本文链接:https://www.haomeiwen.com/subject/digetttx.html