如何存储然后打印二维字符/字符串数组? [英] how to store and then print a 2d character/string array?
本文介绍了如何存储然后打印二维字符/字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有这些词:老虎、狮子、长颈鹿.
Suppose I have the words: tiger, lion, giraffe.
如何使用 for
循环和 scanf
将其存储在二维 char
数组中,然后使用for
循环?
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 抱歉问这么基本的问题,但我在 Google 上找不到合适的答案.
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]);
}
最后为了打印它们使用
for (i=0; i<NUMBER_OF_WORDS; i++) {
printf ("%s" , arrayOfWords[i]);
}
这篇关于如何存储然后打印二维字符/字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文