输入2个字符串,比较它们是否相等。要求定义和调用函数cmp(s,t),该函数比较字符串s和t是否相等,若相等则返回1,否则返回0。 运行示例: Enter 2 strings:hello world ”hello” != ”world” 【程序】 #include <stdio.h> int cmp(char *s,char *t) { int i; for(i=0; _______ 1 ______ ;i++) if( _____ 2 ________ ) break; if( _____ 3 _____ ) return 1; else return 0; } int main() { char s[80],t[80]; printf(”Enter 2 strings: ”); scanf(”%s%s”,s,t); if( ______ 4 _____ ;) printf(”\”%s\” = \”%s\”\n”,s,t); else printf(”\”%s\” != \”%s\”\n”,s,t); }
时间: 2024-02-22 07:57:14 浏览: 18
【程序】
```
#include <stdio.h>
int cmp(char *s, char *t) {
int i;
for(i = 0; s[i] == t[i]; i++) {
if(s[i] == '\0') return 1;
}
return 0;
}
int main() {
char s[80], t[80];
printf("Enter 2 strings: ");
scanf("%s%s", s, t);
if(cmp(s, t)) {
printf("\"%s\" = \"%s\"\n", s, t);
} else {
printf("\"%s\" != \"%s\"\n", s, t);
}
return 0;
}
```
【调用过程】
1. 在定义 `cmp` 函数中,循环条件为 `s[i] == t[i]`,即当两个字符串的当前字符相等时进入循环。
2. 在循环中,判断当前字符是否为字符串结束符 `'\0'`,如果是则说明两个字符串相等,返回1。
3. 在循环结束后,如果还没有返回,则说明两个字符串不相等,返回0。
4. 在 `main` 函数中,调用 `cmp` 函数比较两个字符串是否相等,如果相等则输出相等的信息,否则输出不相等的信息。
相关问题
C语言输入2个字符串,比较它们是否相等。要求定义和调用函数cmp(s,t),该函数比较字符串s和t是否相等,若相等则返回1,否则返回0。
好的,下面是实现该功能的C语言代码:
```c
#include <stdio.h>
#include <string.h>
int cmp(char *s, char *t) {
if (strcmp(s, t) == 0) {
return 1;
}
else {
return 0;
}
}
int main() {
char s[100], t[100];
printf("请输入字符串s: ");
gets(s);
printf("请输入字符串t: ");
gets(t);
if (cmp(s, t)) {
printf("字符串s和字符串t相等\n");
}
else {
printf("字符串s和字符串t不相等\n");
}
return 0;
}
```
首先,程序会提示用户输入两个字符串s和t,然后调用函数cmp(s, t)比较它们是否相等。函数cmp(s, t)使用strcmp函数比较两个字符串s和t是否相等,如果相等则返回1,否则返回0。最后,程序根据函数的返回值输出相应的结果。注意,这里使用了gets函数来输入字符串,可以输入包含空格的字符串。
输入2个字符串,比较它们的大小。要求定义和调用函数cmp(s, t),该函数逐个比较字符串s和t中的对应字符,直到对应字符不等或比较到串尾。若s和t相等则返回0,若不相等则返回不相等字符的差值,即若s大于t则返回一个正数,若s小于t则返回一个负数。输入输出示例如下:
### 回答1:
输入:
hello
world
输出:
-15
输入:
apple
apple
输出:
0
输入:
python
java
输出:
11
代码如下:
def cmp(s, t):
i = 0
while i < len(s) and i < len(t):
if s[i] != t[i]:
return ord(s[i]) - ord(t[i])
i += 1
return len(s) - len(t)
s = input()
t = input()
print(cmp(s, t))
### 回答2:
定义函数cmp(s, t),逐个比较字符串s和t中的对应字符,直到对应字符不等或比较到串尾。若s和t相等则返回0,若不相等则返回不相等字符的差值。若s大于t则返回一个正数,若s小于t则返回一个负数。
示例输入1:
s = "abc"
t = "abc"
示例输出1:
0 (s和t相等)
示例输入2:
s = "abc"
t = "abd"
示例输出2:
-1 (s小于t,差值为-1)
示例输入3:
s = "abc"
t = "ab"
示例输出3:
1 (s大于t,差值为1)
实现代码如下:
```python
def cmp(s, t):
i = 0
while i < len(s) and i < len(t):
if s[i] != t[i]:
return ord(s[i]) - ord(t[i])
i += 1
return len(s) - len(t)
s = input("请输入第一个字符串:")
t = input("请输入第二个字符串:")
result = cmp(s, t)
print("比较结果:", result)
```
这段代码首先定义了cmp函数,然后获取用户输入的两个字符串s和t,并调用cmp函数比较它们的大小。最后打印比较结果。
### 回答3:
要比较两个字符串的大小,可以通过定义并调用函数cmp(s, t)来实现。该函数逐个比较字符串s和t中的对应字符,直到对应字符不等或比较到串尾。
函数的实现如下:
```python
def cmp(s, t):
i = 0
while i < len(s) and i < len(t):
if s[i] != t[i]:
return ord(s[i]) - ord(t[i])
i += 1
return len(s) - len(t)
```
其中,`len(s)`和`len(t)`分别获取字符串s和t的长度。`ord(s[i])`可以获取字符s[i]的ASCII码值。
通过调用这个函数,可以比较两个字符串的大小,如下所示:
```python
s = input("请输入字符串s:")
t = input("请输入字符串t:")
result = cmp(s, t)
if result > 0:
print("s大于t")
elif result < 0:
print("s小于t")
else:
print("s等于t")
```
这样,就能根据比较的结果输出相应的提示信息。