c语言输入字符串的选择结构
时间: 2023-06-09 12:07:47 浏览: 79
好的,您的问题是关于c语言输入字符串的选择结构。通常情况下,我们可以使用scanf函数来输入字符串。具体来说,您可以使用以下方式输入字符串并进行选择结构的判断:
char str[100]; //定义一个字符串数组来存储输入的字符串
scanf("%s", str); //使用scanf函数进行输入字符串操作
if(strcmp(str, "hello") == 0) { //使用strcmp函数进行字符串比较
printf("您输入的字符串是 hello");
}
else if(strcmp(str, "world") == 0) {
printf("您输入的字符串是 world");
}
else {
printf("您输入的是其他字符串");
}
希望能够回答您的问题。
相关问题
c语言链表输入字符串
以下是一个示例,演示了如何在C语言中使用链表输入字符串:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义链表节点结构
struct Node {
char data[100];
struct Node* next;
};
// 在链表末尾插入节点
void insert(struct Node** head, char* data) {
// 创建新节点
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
strcpy(newNode->data, data);
newNode->next = NULL;
// 如果链表为空,将新节点设置为头节点
if (*head == NULL) {
*head = newNode;
return;
}
// 找到链表末尾的节点
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
// 将新节点插入到末尾
current->next = newNode;
}
// 打印链表中的所有字符串
void printList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%s ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
// 输入字符串并插入链表
char input[100];
printf("请输入字符串(输入exit结束):\n"); while (1) {
scanf("%s", input);
if (strcmp(input, "exit") == 0) {
break;
}
insert(&head, input);
}
// 打印链表中的所有字符串
printf("链表中的字符串为:\n");
printList(head);
return 0;
}
```
c语言输入不同的字符串的选择结构怎么写
可以使用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;
}
```