华为机试题(6)

来源:互联网 发布:diskgenius linux版 编辑:程序博客网 时间:2024/06/11 23:10

1、删除子串,只要是原串中有相同的子串就删掉,不管有多少个,返回子串个数。

[cpp] view plaincopyprint?
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.   
  5. int delete_sub_str(const char *str,const char *sub_str,char *result)  
  6. {  
  7.     char *p=str;  
  8.     char *q=sub_str;  
  9.     char *r = result;  
  10.     int sub_len = strlen(sub_str);  
  11.     int cmp,cnt = 0;  
  12.     char tmp[50];  
  13.     while(*p && (p+sub_len-1 != '\0'))  
  14.     {  
  15.         if(*p == *q)  
  16.         {  
  17.             strncpy(tmp,p,sub_len);  
  18.             tmp[sub_len] = '\0';  
  19.             cmp=strcmp(tmp,sub_str);  
  20.             if(cmp == 0)  
  21.             {  
  22.                 cnt++;  
  23.                 p=p+sub_len;  
  24.             }  
  25.               
  26.         }  
  27.         *result++ = *p;  
  28.         p++;  
  29.     }  
  30.     *result = '\0';  
  31.     return cnt;  
  32.   
  33. }  
  34. int main(/*int argc, char **argv*/)  
  35. {  
  36.     int cnt;  
  37.     const char str[50]="hao123hello123world";  
  38.     const char sub_str[50]="123";  
  39.     char result[50];  
  40.     cnt = delete_sub_str(str,sub_str,result);  
  41.   
  42.     printf("result is :%s\n ",result);  
  43.     printf("cnt is :%d\n ",cnt);  
  44.     getchar();  
  45.     return 0;  
  46. }  

2 使用C语言实现字符串中子字符串的替换

描述:编写一个字符串替换函数,如函数名为 StrReplace(char* strSrc, char* strFind, char* strReplace),strSrc为原字符串,strFind是待替换的字符串,strReplace为替换字符串。

举个直观的例子吧,如:“ABCDEFGHIJKLMNOPQRSTUVWXYZ”这个字符串,把其中的“RST”替换为“ggg”这个字符串,结果就变成了:

ABCDEFGHIJKLMNOPQgggUVWXYZ

[cpp] view plaincopyprint?
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.   
  5. void StrReplace(char* strSrc, char* strFind, char* strReplace)  
  6. {  
  7.     char *p=strSrc;  
  8.     char *q=strFind;  
  9.     char *s=strReplace;  
  10.     char result[50];  
  11.     char tmp[50];  
  12.     char *r=result;  
  13.     int cmp;  
  14.     int len = strlen(strFind);  
  15.     //int len2=strlen(strSrc);  
  16.     while(*p )  
  17.     {  
  18.         if(*p == *q)  
  19.         {  
  20.             strncpy(tmp,p,len);  
  21.             tmp[len]='\0';  
  22.             cmp = strcmp(tmp,strFind);  
  23.             if(cmp == 0)  
  24.             {  
  25.                 while(*s)  
  26.                     *r++ = *s++;  
  27.                 p=p+len;  
  28.                 if(*p=='\0')  
  29.                     break;  
  30.                 continue;  
  31.             }  
  32.         }  
  33.         *r++ = *p;  
  34.         p++;  
  35.     }  
  36.     *r = '\0';  
  37.     strcpy(strSrc,result);  
  38. }  
  39. int main(/*int argc, char **argv*/)  
  40. {  
  41.       
  42.     char strSrc[50]="abcdhelloabefg";  
  43.     char strFind[50]="hello";  
  44.     char strReplace[50]="world";  
  45.     printf("result is :%s\n ",strSrc);  
  46.     StrReplace(strSrc,strFind,strReplace);  
  47.     printf("result is :%s\n ",strSrc);  
  48.     //printf("cnt is :%d\n ",cnt);  
  49.     getchar();  
  50.     return 0;  
  51. }  
原创粉丝点击