1.題目
輸入2個字符串,分別用
void str_cat(char*p1,char*p2);int str_cmp(char*p1,char*p2);int str_len(char*p1,char*p2)
輸出2個字符串的比較結果,拼接字符串和其長度。
2.我的代碼
```#include#includeusing namespace std;void str_cat(char*p1,char*p2);int str_cmp(char*p1,char*p2);int str_len(char*p1,char*p2);int main(){const int s=20;char str1[s];char str2[s];cin.getline(str1,s);cin.getline(str2,s);char* p1,p2;p1=str1;p2=str2;str_cat(p1,p2);str_cmp(p1,p2);str_len(p1);}void str_cat(char*p1,char*p2){cout<<strcat(p1,p2);}int str_cmp(char*p1,char*p2){cout<<strcmp(p1,p2);return 0;}int str_len(char*p){return strlen(p);}
請問我錯在那些地方,麻煩給我寫出來,萬分感謝!!!!!!!!!!!!!!!!!!!!!!!!!**要求運行後顯示
1.兩個字符串比較後的結果
2.拼接後的字符串
3.其長度**
void str_cat(char*p1,char*p2)
{
while (*p1 != '\0') p1++;
int i = 0;
while (p2[i++] != '\0')
{
*p1 = p2[i - 1];p1++;
}
*p1 = '\0';
}
int str_cmp(char*p1,char*p2)
{
while (*p1 != '\0' && *p2 != '\0')
{
if (*p1 != * p2) return *p1 - *p2;
p1++;p2++;
}
return *p1 - *p2;
}
int str_len(char*p1,char*p2)
{
int n = 0;
while (*p1 != '\0') { p1++; n++; }
while (*p2 != '\0') { p2++; n++; }
return n;
}
int main()
{
char s1[100]="hello ";
char s2[]="world!";
str_cat(s1, s2);
printf("%s\n", s1);
int n = str_cmp("aaa", "ab");
printf("%d\n", n);
n = str_len("aaa", "ab");
printf("%d\n", n);
}