华为机试题(6)
来源:互联网 发布:diskgenius linux版 编辑:程序博客网 时间:2024/06/11 23:10
1、删除子串,只要是原串中有相同的子串就删掉,不管有多少个,返回子串个数。
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int delete_sub_str(const char *str,const char *sub_str,char *result)
- {
- char *p=str;
- char *q=sub_str;
- char *r = result;
- int sub_len = strlen(sub_str);
- int cmp,cnt = 0;
- char tmp[50];
- while(*p && (p+sub_len-1 != '\0'))
- {
- if(*p == *q)
- {
- strncpy(tmp,p,sub_len);
- tmp[sub_len] = '\0';
- cmp=strcmp(tmp,sub_str);
- if(cmp == 0)
- {
- cnt++;
- p=p+sub_len;
- }
- }
- *result++ = *p;
- p++;
- }
- *result = '\0';
- return cnt;
- }
- int main(/*int argc, char **argv*/)
- {
- int cnt;
- const char str[50]="hao123hello123world";
- const char sub_str[50]="123";
- char result[50];
- cnt = delete_sub_str(str,sub_str,result);
- printf("result is :%s\n ",result);
- printf("cnt is :%d\n ",cnt);
- getchar();
- return 0;
- }
#include <stdio.h>#include <stdlib.h>#include <string.h>int delete_sub_str(const char *str,const char *sub_str,char *result){char *p=str;char *q=sub_str;char *r = result;int sub_len = strlen(sub_str);int cmp,cnt = 0;char tmp[50];while(*p && (p+sub_len-1 != '\0')){if(*p == *q){strncpy(tmp,p,sub_len);tmp[sub_len] = '\0';cmp=strcmp(tmp,sub_str);if(cmp == 0){cnt++;p=p+sub_len;}}*result++ = *p;p++;}*result = '\0';return cnt;}int main(/*int argc, char **argv*/){int cnt;const char str[50]="hao123hello123world";const char sub_str[50]="123";char result[50];cnt = delete_sub_str(str,sub_str,result);printf("result is :%s\n ",result);printf("cnt is :%d\n ",cnt);getchar();return 0;}
2 使用C语言实现字符串中子字符串的替换
描述:编写一个字符串替换函数,如函数名为 StrReplace(char* strSrc, char* strFind, char* strReplace),strSrc为原字符串,strFind是待替换的字符串,strReplace为替换字符串。
举个直观的例子吧,如:“ABCDEFGHIJKLMNOPQRSTUVWXYZ”这个字符串,把其中的“RST”替换为“ggg”这个字符串,结果就变成了:
ABCDEFGHIJKLMNOPQgggUVWXYZ
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void StrReplace(char* strSrc, char* strFind, char* strReplace)
- {
- char *p=strSrc;
- char *q=strFind;
- char *s=strReplace;
- char result[50];
- char tmp[50];
- char *r=result;
- int cmp;
- int len = strlen(strFind);
- //int len2=strlen(strSrc);
- while(*p )
- {
- if(*p == *q)
- {
- strncpy(tmp,p,len);
- tmp[len]='\0';
- cmp = strcmp(tmp,strFind);
- if(cmp == 0)
- {
- while(*s)
- *r++ = *s++;
- p=p+len;
- if(*p=='\0')
- break;
- continue;
- }
- }
- *r++ = *p;
- p++;
- }
- *r = '\0';
- strcpy(strSrc,result);
- }
- int main(/*int argc, char **argv*/)
- {
- char strSrc[50]="abcdhelloabefg";
- char strFind[50]="hello";
- char strReplace[50]="world";
- printf("result is :%s\n ",strSrc);
- StrReplace(strSrc,strFind,strReplace);
- printf("result is :%s\n ",strSrc);
- //printf("cnt is :%d\n ",cnt);
- getchar();
- return 0;
- }
- 华为机试题(6)
- 华为机试题(6)
- 华为机试题(2)
- 华为机试题(3)
- 华为机试题(4)
- 华为机试题(5)
- 华为机试题(7)
- 华为机试题 (1)
- 华为机试题(2)
- 华为机试题(3)
- 华为机试题(4)
- 华为机试题(5)
- 华为机试题(7)
- 华为机试题(二)
- 华为机试题【6】-大小写转换
- 华为2道机试题(review)
- 华为机试题(11)--约瑟夫问题
- 华为机试题(字符串处理集锦)
- Glibc 的malloc源代码分析
- Linux内核学习笔记之网卡驱动的详细分析:RTL8139
- 华为机试题(5)
- <win8>(五)实例讲解win8(XAML+C#)开发--------课程表:Appbar,FilePicker,启动页面(动画)
- SDN & OpenFlow 2
- 华为机试题(6)
- 用Floyed算法求最短路径
- 华为机试题(7)
- KeUserModeCallback用法详解
- 【原创】NTFS文件系统底层挖掘
- 小高斯与老师的故事,我觉得老师数学功底完爆小高斯一条街。
- Android的frameworks层键盘事件处理流程分析
- 华为机试题(9)--将两个数从字符串转为数,将这两个数做乘积后再转化为字符串保存起来
- Objective-C的消息机制