几个字符串相关的题目

今天整理了几个老师讲过的字符串的题目

1.求一个字符串中数字出现的个数和最大数:

例如输入ab12cd123ef1234;输出结果 max = 3,最大整数为1234;

程序如下:

#include 

#define MAX_SIZE 100

int main()
{
    int max = 0;
    int i;
    int k = 0;
    int count = 0;
    int j = 0;
    
    
    char str[MAX_SIZE];
    char src[MAX_SIZE];
    
    printf("please input the string:\n");

    scanf("%s",src);
    printf("src=%s\n",src);

    for(i = 0; src[i] != '\0'; i++)
    {
        while(src[i] >= '0' && src[i] <= '9')
	{
	     count++;
	     i++;
	}
    
        if(count > max)
        {
             max = count;

	     for(j =i - count; j < i;j++)
	     {
	         str[k] = src[j];
		 k++;
	     }

	     str[k] = '\0';
        }
	

	count = 0;
	k = 0;

    }
   

    printf("max=%d\n",max);
    printf("str=%s\n",str);
    return 0;

}

2.进行字符串逆置,但是只进行单词逆置,比如I am from shanghai 逆置后为 iahgnahs morf ma I 然后再每个单词逆置为 shanghai from am I;

程序如下:

#include 

#include 

#define MAX 100

void reverser_string(char *src, int len)
{
    int i;
    char temp;

    for(i = 0; i < len / 2; i++)
    {
        temp = *(src+i);
	*(src+i) = *(src+len-i-1);
	*(src+len -i-1) = temp;
    }

    printf("src=%s\n",src);
}

void reverser_sentence(char *src)
{
    int len = 0;
    while(*src != '\0')
    {
        if(*src ==' ')
	{
	    reverser_string(src-len,len);
	    len = 0;
	}
	    else 
	{
	    len++;
	}

	src++;
    }

    reverser_string(src-len,len);
}

int main()
{
    char src[MAX];

    printf("Please input the string:\n");
    gets(src);

    reverser_string(src,strlen(src));
    
    reverser_sentence(src);

    printf("src=%s\n",src);

    return 0;
}

3.找出字符串中子串出现个数:

程序如下:

#include 

#include 

int find_count(char *s1, char *s2)
{
    int len;
    int count = 0;

    len = strlen(s2);

    while(*s1 != '\0')
    {
        if(my_strnmp(s1,s2,len) == 0)
	{
	    count++;
	    s1 = s1 + len;
	}
	else
	{
	    s1++;
	}
    }

    return count;
}

int my_strnmp(char *src, char *str)
{
    int i;

    for(i = 0; i < strlen(str); i++)
    {
        if( *(src + i) != *(str + i))
	{
	    return -1;
	}
    }

    return 0;
}

int main()
{
    char src[100];
    char str[100];

    printf("please input src:\n");
    scanf("%s",src);

    printf("please input str:\n");
    scanf("%s",str);

    int count = find_count(src,str);

    printf("count = %d\n",count);
}

4.自己编写的strcmp,strlen等功能的函数:

程序如下:

#include 

char *my_strcnpy(char *dest, char *src, int len)
{
   int i;

   char *temp = dest;

   for(i = 0; i < len; i++)
   {
       *(temp+i) = *(src+i);
   }

    return dest;
}

char *my_strncat(char *dest, char *src, int len)
{   
    int i;

    char *temp;
    
    temp = dest;

    while(*temp != '\0')
    {
        temp++;
    }

    for(i = 0; i < len; i++)
    {
        *temp = *src;
	src++;
	temp++;
    }

    *temp = '\0';

    return dest;
}

int my_strlen(char *src)
{
    int len = 0;
    
    while(*src != '\0')
    {
        src++;

	len++;
    }

    return len;
}


int my_strcmp(char *dest, char *src)
{
    while(*dest != '\0' && *src != '\0')
    {
        if(*dest > *src)
	{
	    return -1;
	}
	else if(*dest < *src)
	{
	    return 1;
	}

	dest++;
    }

    return 0;
}

int main()
{
    char src[100] = "hello world";
    char str[100] = "hello world";

    //int len;

    //len = my_strlen(src);

    //printf("len = %d\n",len);
    
    int ret =  my_strcmp(src,str);

    printf("ret = %d\n", ret);

    return 0;
}



你可能感兴趣的:(几个字符串相关的题目)