strstr函数的被查找字符串与查找到的字符串是互相影响的,同时可以只用i--控制循环不变
来源:互联网 发布:北京黑马程序员地址 编辑:程序博客网 时间:2024/06/04 23:33
这题刚开始写出来之后一直不知道哪里错了,就一部一部的dbug,最后终于发现原来strstr的被查找字符串改变会影响查找到的
字符串,感觉自己写的不够简便,查了一些别人的,看到别人是用strstr函数的查找到的字符串会影响被查找的字符串的性质来写的
,代码中注释部分是用被查找影响查找字符串的性质做的,比较麻烦,下面是用其逆性质来写的,比较简单,另外注意此题使用i--控制
循环不变而不需要使用死循环:
讲解下strstr的性质:注释部分是输出
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<set>
using namespace std;
int main()
{
char s[50]="abcdfg";
char s1[50]="abc";
char *p=strstr(s,s1);
strcat(s,"higk");
printf("%s\n",p);//abcdefghigk
strcat(p,"lmn");
printf("%s\n",s);//abcdefghigklmn
return 0;
}
题目代码:
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<cctype>#include<cmath>#include<set>using namespace std;char s[20][100],s1[20][100],s2[300];int main(){ int t; while(cin>>t&&t) { getchar(); for(int i=0;i<t;i++) { gets(s[i]); gets(s1[i]); } gets(s2); for(int i=0;i<t;i++) { /* while(1) { if(strstr(s2,s[i])!=NULL) { char *ss; ss=strstr(s2,s[i]); int len=strlen(s2)-strlen(ss); char sst[300]; strcpy(sst,s2); if(len!=0) { sst[len]='\0'; strcat(sst,s1[i]); strcat(sst,ss+strlen(s[i])); } else { sst[0]='\0'; strcat(sst,s1[i]); strcat(sst,ss+strlen(s[i])); } strcpy(s2,sst); } else break; }*/ char *p=strstr(s2,s[i]); char temp[300]; if(p!=NULL) { strcpy(temp,p+strlen(s[i])); strcpy(p,s1[i]); strcpy(p+strlen(s1[i]),temp); i--; } } printf("%s\n",s2); } return 0;}
0 0
- strstr函数的被查找字符串与查找到的字符串是互相影响的,同时可以只用i--控制循环不变
- strstr字符串查找函数
- strstr的使用(字符串的查找)
- 有意思的字符串查找函数strchr,strrchr,strstr,strrstr
- 查找完全匹配的子字符串strstr
- 字节序列查找bytesBytes 和 字符串查找strstr函数的实现代码
- php strstr查找字符串中是否包含某些字符的查找函数
- php strstr查找字符串中是否包含某些字符的查找函数
- 在不用strstr()函数的情况下在字符串中查找某一子字符串
- 字符串比较函数strncasecmp():比较字符串的前n个字符|字符串查找函数strstr和string.find() 查找字符串str1中是否存在与str2一样的子字符串
- 查找字符/字符串的函数
- 一个字符串查找的函数
- 字符串的查找与替换
- 字符串的“循环平移”和单词查找
- 用kmp查找字符串的循环节
- 分享给大家两个不区分大小写的字符串查找函数 strstr
- php中常用的字符串查找函数strstr()、strpos()实例解释
- 字符串查找-strStr
- ZooKeeper权限控制
- char,short ,int ,long,long long,unsigned long long数据范围
- 银联加密算法
- JS
- 进程和线程的关系和区别
- strstr函数的被查找字符串与查找到的字符串是互相影响的,同时可以只用i--控制循环不变
- POJ题目2585Window Pains(拓扑排序)
- PAT (Advanced Level) 1015. Reversible Primes (20) 翻转质数
- 容器改变着开发规则
- 深入理解JAVA虚拟机笔记 - 02
- PyQt之QHBoxLayout、QVBoxLayout 与QGridLayout
- springmvc 入门环境搭建
- 算法之枚举思想
- LinxC基础笔试题