用C++ 编写一个程序:提示用户输入两个字符串,然后比较这两个字符串是否相等。求教了

不用那个C++的标准库,请教了

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void Replace(char *s,char *t,char *v) //对s采用char s[]等同于char *s 传递值 *为取消引用
{
if(strlen(t)!=strlen(v))
{ printf("The length of the two string you entered are not the same \n"); exit(0); }
int slen=strlen(s);
int tlen=strlen(t);
int i=0,j=0;
while(i<slen) //没到达s的末尾则比较
{
while(i<slen && j<tlen)
{
if(s[i]==t[j]) {i++; j++;} //如果相同则i,j加1
else {i=i-j+1; j=0;} //否则i退回到i-j+1
}

if(j>=tlen) //如果找到子串则进行替换
{
int k=i-tlen;
for(j=0;j<tlen;k++,j++)
{s[k]=v[j];}

}
j=0; //对t的指针以0赋值 从新开始比较
if(i>=slen)
{printf("主串中没有输入的子串\n");exit(0);}

}

}

int main()
{
char str[50],sstr[50],rstr[50];

printf("Enter the main string please\nthen I will find the place where the second string in it\n");
scanf("%s",str);
puts(str);
printf("\nEnter the sub string, and the the string you want to replace :\n");
scanf("%s%s",sstr,rstr);
Replace(str,sstr,rstr);
printf("%s\n替换成功\n",str);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-04-07
#include <iostream>

using namespace std;
int main()
{
char str1[100],str2[100];
cout<<"输入字符串1:";
cin>>str1;
cout<<"输入字符串2:";
cin>>str2;
if (strlen(str1) != strlen(str2))//如果两个字符串长度不等,当然不相等
cout<<"不相等!"<<endl;
else
{
for(int i=0; i<strlen(str1); i++)
{
if (str1[i] != str2[i])
{
cout<<"不同!"<<endl;//如果两个字符串当中有一个不相同的字符,则不相等,退出循环
break;
}
if (i == strlen(str1) - 1)//如果字符串比较到最后一个字符都相同,那么相等
cout << "相同!" << endl;
}
}
return 0;
}
第2个回答  2009-04-07
#include <iostream>
#include<string>
using namespace std;
int main()
{
string str1,str2;
cout<<"输入字符串1:";
cin>>str1;
cout<<"输入字符串2:";
cin>>str2;
if(str1==str2)
cout<<"相同"<<endl;
else cout<<"不同"<<endl;
return 0;
}
第3个回答  推荐于2016-06-02
#include <iostream.h>
#include <string.h>
int main()
{
char a[255] = {'\0'}, b[255] = {'\0'};
cout << "input first string: ";
cin >> a;
cout << "input second string: ";
cin >> b;
int i;
if(strlen(a) == strlen(b))
{
for(i = 0; i < (int)strlen(a); i++)
if(a[i] != b[i])
break;
}
if(i == (int)strlen(a))
cout << "a equal to b" << endl;
else
cout << "a not equal to b" << endl;
return 0;
}本回答被提问者采纳
第4个回答  2009-04-07
这么简单的问题就悬赏50分 楼主你的分太多了吧
相似回答