c语言输入不同的字符串的选择结构怎么写
时间: 2023-12-09 16:06:05 浏览: 23
可以使用if-else语句或switch语句来实现输入不同的字符串的选择结构。
使用if-else语句:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[20];
printf("请输入一个字符串:");
scanf("%s", str);
if (strcmp(str, "hello") == 0) {
printf("你输入的是hello\n");
} else if (strcmp(str, "world") == 0) {
printf("你输入的是world\n");
} else {
printf("你输入的是其他字符串\n");
}
return 0;
}
```
使用switch语句:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[20];
printf("请输入一个字符串:");
scanf("%s", str);
switch (strcmp(str, "hello")) {
case 0:
printf("你输入的是hello\n");
break;
case -1:
printf("你输入的字符串在hello之前\n");
break;
case 1:
printf("你输入的字符串在hello之后\n");
break;
}
return 0;
}
```