【发布时间】:2019-07-24 10:53:43
【问题描述】:
我想做的是逐个字符地比较两个字符串。
示例 1:
字符串 A:你好
字符串 B:HelloWorld
输出:你好
示例 2:
字符串 A:你好
弦B:吹嘘
输出:老旧
在上面的例子中,即使字符串 A 比字符串 B 有更多的字符,反之亦然,而且它们都是不同的,我仍然希望在两个相同的字符串上输出字符。一旦正在比较的字符串之一达到空值,我还希望程序停止比较。
我已经想出了几行代码可能可以做到这一点,但我不太确定我是否朝着正确的方向前进。
int main(){
char stra[100];
char strb[100];
char strc[100];
for(int i = 0; stra[i] != Null && strb[i] != Null; i++){
if(stra[i] == strb[i]){
strc[i] = stra[i];
}
}
if (strc != NULL){
printf(strc);
}else{
printf("both words do not match");
}
}
【问题讨论】:
-
I would still want to output the characters on both strings that are the same.-->这个输入呢?A: abcdgh and B: dcbaefgh是否应该输出abcdh? -
输出应该是 gh
-
你怎么解释?
-
您正在比较未初始化的字符串。
Null是什么? -
ABCDEFG和XBXXDEFY想要什么输出?BDEF?我认为您的规范不完整。
标签: c string char max string-comparison