How to loop and input multiple strings in the C language.

In C language, you can use loop structures and character arrays to input multiple strings in a loop. Here is an example code that loops to input 5 strings and then prints them out.

#include <stdio.h>

int main() {
    char str[5][50]; // 定义一个二维字符数组用于存储多个字符串

    printf("请输入5个字符串:\n");
    
    // 循环输入5个字符串
    for(int i = 0; i < 5; i++) {
        printf("输入第%d个字符串:", i+1);
        scanf("%s", str[i]);
    }

    // 打印输出输入的5个字符串
    printf("输入的5个字符串为:\n");
    for(int i = 0; i < 5; i++) {
        printf("%s\n", str[i]);
    }

    return 0;
}

In the code above, we define a two-dimensional character array called ‘str’ to store 5 strings. We then use a loop and the scanf function to input these 5 strings, followed by another loop and the printf function to print out the strings. You can modify the array size and loop count as needed to input more strings.

bannerAds