[Exercises] algorithm notes problems D: Replace the word (not a replacement string)

The idea is not to replace the string, only needs to match (Find) string, when matched to the output string after replacement of the original continues to output.

Simple method that does not involve insertion and deletion of the string,

That string search string as long as the need to query went '\ 0'

 

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
 
char str[101], a[101],b[100];
	
int main()
{
	
	while(gets(str)){
		gets(a);
		gets(b);
		
		int lena = strlen(a), lenb = strlen(b);//记录长度
		int j,i,k;
		for(int i=0; str[i] != '\0'; i++) 
		{
				//查找有没有此字符串,如果不等就跳出循环,如果等于,当前i就是齐字符串起始位置 
				for(k=i, j=0; a[j]!='\0' ; j++,k++ ) 
				{
					if(a[j] != str[k])
						break;
				}
				//如果有相同的字符串,输出b串 
				if(a[j] =='\0' )
				{
					printf("%s",b);
					i+=lena-1;
				}
				//没有匹配的
				else{
					putchar(str[i]);
				} 
			 
		}
		printf("\n");
		
	}	
	
	
	
 	return 0;
}      

 

Published 63 original articles · won praise 13 · views 40000 +

Guess you like

Origin blog.csdn.net/changreal/article/details/88358054