如何使用循环扫描字符串然后将这些字存储在数组中?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用循环扫描字符串然后将这些字存储在数组中?相关的知识,希望对你有一定的参考价值。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main ()
char word[100][21] ;
puts( "Enter Your Words" );
puts( "Enter STOP To Get Your Results" );
while( strcmp( word, "STOP" ) )
scanf( "%20s", word );
return 0;
在我扫描一个单词id之后将其存储到一个名为storing[][]
的数组中,但我不知道如何实现它,而且我也不想存储终止的STOP
字
答案
编码你想要的。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define WORD_MAX 100
int main (void)
char storing[WORD_MAX][21]; /* you want the words stored into storing[][], not word */
char buffer[21]; /* a buffer to store the word temporaly for not to store STOP to storing */
int wordCount = 0; /* count how many words are stored */
puts( "Enter Your Words" );
puts( "Enter STOP To Get Your Results" );
/* loop while there is room to store new word left in the array,
* successfully read something and what is read is not the STOP word */
while(wordCount < WORD_MAX && scanf("%20s", buffer) == 1 && strcmp(buffer, "STOP") != 0)
/* store the word read and increment the count */
strcpy(storing[wordCount++], buffer);
/* sample code for testing: print what is read */
int i;
for (i = 0; i < wordCount; i++) printf("%03d : %s\n", i, storing[i]);
return 0;
以上是关于如何使用循环扫描字符串然后将这些字存储在数组中?的主要内容,如果未能解决你的问题,请参考以下文章