Thay thế
1 từ trong 1 câu bằng 1 từ khác
#include<iostream.h>
#include<string.h>
void main()
{
//Code design bby SEO ANH
char cau[100]="",chuoi1[50]="",chuoi2[50]="",temp[50]="",truoc[50]="",sau[50]="";
int i,tem;
cout<<"\n\nNhap 1 cau: ";cin.getline(cau,100);
cout<<"\nNhap tu muon thay: ";cin.getline(chuoi1,50);
cout<<"\nNhap tu de thay : ";cin.getline(chuoi2,50);
cout<<"\n\nCau truoc khi thay: \n"<<cau;
strcpy(temp,strstr(cau,chuoi1));
tem=strlen(temp);
i=strlen(cau)-strlen(temp)-1;
strncpy(truoc,cau,i>0?i:0);
strcpy(sau,strstr(temp," ")?strstr(temp," "):"");
strcpy(cau,strcat(strcat(strcat(truoc,i>0?" ":""),chuoi2),sau));
cout<<"\n\nCau sau khi thay: \n"<<cau<<endl<<endl;
}
Nhãn bài viết: Chia-se | Hoc-tap