rt,如何比较2个字符数组是否相等?
这个应该挺简单吧 如果是string型 有个比较函数cmp(string a,string b)
如果是char 型 就用个循环 前提是两个数组长度一样
例: char a[n],b[n]
int s=0;
for(int i=0;i<n;i++)
{
if(a[i]==b[i]) {s++;} //s是对某位上相等的进行计数;
} //s如果与n相等就说明数组相等,反之不是
如果是char 型 就用个循环 前提是两个数组长度一样
例: char a[n],b[n]
int s=0;
for(int i=0;i<n;i++)
{
if(a[i]==b[i]) {s++;} //s是对某位上相等的进行计数;
} //s如果与n相等就说明数组相等,反之不是
温馨提示:答案为网友推荐,仅供参考
第1个回答 2011-07-06
首先,C必然定义为字符数组,C语言没有定义字符串的关键字,C语言用字符数组处理字符串。如果需要动态长度字符串必须用字符指针实现。我写了一个类似的程序。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void) {
const int SIZE_INC=16;
char *a="abcd";
char *b="bcdef";
char *astr, *cptr;
char ch, ich;
int csize=0, cread=0;
// 读入未知长度字符串,以回车或者EOF结束
printf("Input a string:\n");
cptr = astr = (char *)malloc(SIZE_INC);
csize = SIZE_INC;
ich = getchar();
for (;;) {
if (ich == '\n' || ich == EOF)
ch = '\0';
else
ch = ich;
if (cread == csize) {
astr = (char *)realloc(astr, csize + SIZE_INC);
csize += SIZE_INC;
cptr = astr + cread;
}
*cptr = ch;
if (ch == '\0') break;
cread++; cptr++;
ich = getchar();
}
if (!strcmp(astr,a))
printf("The string you input equals string a.\n");
else if (!strcmp(astr,b))
printf("The string you input equals string b.\n");
else
printf("Your string is: %s\n",astr);
system("pause");
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void) {
const int SIZE_INC=16;
char *a="abcd";
char *b="bcdef";
char *astr, *cptr;
char ch, ich;
int csize=0, cread=0;
// 读入未知长度字符串,以回车或者EOF结束
printf("Input a string:\n");
cptr = astr = (char *)malloc(SIZE_INC);
csize = SIZE_INC;
ich = getchar();
for (;;) {
if (ich == '\n' || ich == EOF)
ch = '\0';
else
ch = ich;
if (cread == csize) {
astr = (char *)realloc(astr, csize + SIZE_INC);
csize += SIZE_INC;
cptr = astr + cread;
}
*cptr = ch;
if (ch == '\0') break;
cread++; cptr++;
ich = getchar();
}
if (!strcmp(astr,a))
printf("The string you input equals string a.\n");
else if (!strcmp(astr,b))
printf("The string you input equals string b.\n");
else
printf("Your string is: %s\n",astr);
system("pause");
return 0;
}
第2个回答 2011-07-05
int cmp(char * str0, char * str1)
{
int i;
for(i=0;str0[i]!=0 && str1[i]!=0 && str0[i]==str1[i];i++);
return str0[i]-str1[i];
}
//返回值为0就表示相等,否则不等本回答被提问者采纳
{
int i;
for(i=0;str0[i]!=0 && str1[i]!=0 && str0[i]==str1[i];i++);
return str0[i]-str1[i];
}
//返回值为0就表示相等,否则不等本回答被提问者采纳
第3个回答 2011-07-05
首先,C必然定义为字符数组,C语言没有定义字符串的关键字,C语言用字符数组处理字符串。如果需要动态长度字符串必须用字符指针实现。我写了一个类似的程序。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void) {
const int SIZE_INC=16;
char *a="abcd";
char *b="bcdef";
char *astr, *cptr;
char ch, ich;
int csize=0, cread=0;
// 读入未知长度字符串,以回车或者EOF结束
printf("Input a string:\n");
cptr = astr = (char *)malloc(SIZE_INC);
csize = SIZE_INC;
ich = getchar();
for (;;) {
if (ich == '\n' || ich == EOF)
ch = '\0';
else
ch = ich;
if (cread == csize) {
astr = (char *)realloc(astr, csize + SIZE_INC);
csize += SIZE_INC;
cptr = astr + cread;
}
*cptr = ch;
if (ch == '\0') break;
cread++; cptr++;
ich = getchar();
}
if (!strcmp(astr,a))
printf("The string you input equals string a.\n");
else if (!strcmp(astr,b))
printf("The string you input equals string b.\n");
else
printf("Your string is: %s\n",astr);
system("pause");
return 0;
}
另外,站长团上有产品团购,便宜有保证
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void) {
const int SIZE_INC=16;
char *a="abcd";
char *b="bcdef";
char *astr, *cptr;
char ch, ich;
int csize=0, cread=0;
// 读入未知长度字符串,以回车或者EOF结束
printf("Input a string:\n");
cptr = astr = (char *)malloc(SIZE_INC);
csize = SIZE_INC;
ich = getchar();
for (;;) {
if (ich == '\n' || ich == EOF)
ch = '\0';
else
ch = ich;
if (cread == csize) {
astr = (char *)realloc(astr, csize + SIZE_INC);
csize += SIZE_INC;
cptr = astr + cread;
}
*cptr = ch;
if (ch == '\0') break;
cread++; cptr++;
ich = getchar();
}
if (!strcmp(astr,a))
printf("The string you input equals string a.\n");
else if (!strcmp(astr,b))
printf("The string you input equals string b.\n");
else
printf("Your string is: %s\n",astr);
system("pause");
return 0;
}
另外,站长团上有产品团购,便宜有保证