如何存储并打印2D字符/字符串数组? [英] how to store and then print a 2d character/string array?

查看:228
本文介绍了如何存储并打印2D字符/字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假如我有话:老虎,狮子,长颈鹿

Suppose I have the words: tiger, lion, giraffe.

我怎样才能将其存储在一个二维字符使用数组循环和 scanf函数,然后用循环打印的话一一?

How can I store it in a two dimensional char array using for loop and scanf and then print the words one by one using a for loop?

for(i=0;i<W;i++)
{
    scanf("%s",str[i][0]);  //to input the string
}

PS对不起,问这样一个基本的问题,但我找不到在谷歌一个合适的答案。

PS Sorry for asking such a basic question, but I couldn't find a suitable answer on Google.

推荐答案

首先你需要创建一个字符串数组。

First you need to create an array of strings.

char arrayOfWords[NUMBER_OF_WORDS][MAX_SIZE_OF_WORD];

然后,您需要输入字符串到数组

Then, you need to enter the string into the array

int i;
for (i=0; i<NUMBER_OF_WORDS; i++) {
    scanf ("%s" , arrayOfWords[i]);
}

终于在oreder打印它们使用

Finally in oreder to print them use

for (i=0; i<NUMBER_OF_WORDS; i++) {
    printf ("%s" , arrayOfWords[i]);
}

这篇关于如何存储并打印2D字符/字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆