NYOJ 113 字符串替换

113-字符串替换

题目描述:

编写一个程序实现将字符串中的所有"you"替换成"we"

输入描述:

输入包含多行数据 

每行数据是一个字符串,长度不超过1000 
数据以EOF结束

输出描述:

对于输入的每一行,输出替换后的字符串

样例输入:

复制
you are what you do

样例输出:

we are what we do
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	char ch[1001];
	int l,i,j;
	while(gets(ch))
	{
		l=strlen(ch);
		for(i=0;i<l;i++)
		{  
            if(ch[i]=='y' && ch[i+1]=='o'&& ch[i+2]=='u')
			{  
                ch[i]='w'; ch[i+1]='e';  
                for(j=i+2; ch[j+1]!='\0'; j++)
				{  
                    ch[j] = ch[j+1];  
                }  
                ch[j]='\0';  
            }  
            
        } 
		cout<<ch<<endl; 
	}
	return 0;
}
#include<iostream>
#include<string.h>
using namespace std;
int main() 
{
    char s[1020];
    while(gets(s)) 
	{
        int l=strlen(s);
        for(int i=0; i<l; i++) 
		{
            if(s[i]=='y'&&s[i+1]=='o'&&s[i+2]=='u') 
			{
                s[i]='w';
                s[i+1]='e';
                s[i+2]='0';
            }
        }
        for(int i=0;i<l;i++) 
		{
            if(s[i]!='0')
                cout<<s[i];
        }
        cout<<endl;
    }
    return 0;
}





猜你喜欢

转载自blog.csdn.net/tingtingyuan/article/details/80639608
113