[英]Get words from string array in c using whitespace character
我试过做一个程序,用用户指定的字符串填充字符串数组,然后我遇到一个问题,即如何检查数组中有多少个单词。 我检查了空白字符,但我想它不能很好地工作。由于数组包含的内容,它都会写入flase值:例如,当其中只有1个单词时,它包含2个单词。
int db =0;
char array[255];
void string(char str[]){
printf("Enter a string:");
scanf("%s",str);
}
void words(char str[]){
int i=0;
int len = strlen(str);
while( str[i]!=len)
{
if(str[i]==' ')
db++;
i++;
}
}
int main(int argc,char** argv){
string(array);
words(array);
printf("its contain %d words",db);
}
分析main()函数
int main(int argc,char** argv){
string(array);
words(array);
printf("its contain %d words",db);
}
当您调用words(array) function
, array
什么? 没有。 如果您将array
作为全局变量,那么传递给函数并捕获str
的需求是什么?
下一个,
void string(char str[]){
printf("Enter a string:");
scanf("%s",str);
}
如果要扫描带有空格的字符串,请如下修改上述功能。
void string(char str[]){
int len;//define it
fgets(str,len,stdin);//TO READ STRINGS WITH WHITESPACES
}
接下来是您的逻辑,您想用while( str[i]!=len)
什么? 将循环向上旋转至NULL&,然后修改条件以找到单词数,如下所示。
void words(char str[]){
int i=0;
// int len = strlen(str);
while( str[i]! = NULL)
{
if(str[i]==' ' && str[i+1] !=' ')
db++;
i++;
}
}
完成程序为
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void words(char str[]);
int db =0;
char array[255];
void string(char str[]){
int n;// define it
printf("Enter a string:");
fgets(str,n,stdin);// now data is there in str not in array variable thats why call words() function from here only it's a better option
words(str);// call from here bcz now you have str
}
void words(char str[]){
int i=0;
printf("string = %s \n",str);
while( str[i] != '\0')
{
if( (str[i]==' ' && str[i+1] !=' ') || str[i+1] == '\0')
db++;
i++;
}
printf("db = %d \n",db);
}
int main(int argc,char** argv){
string(array);
printf("its contain %d words",db);
}
我希望这会有所帮助。
您有一些问题,但是主要是使用scanf
而不是gets
。 如果运行此函数,您会发现scanf
无法读取超出空格的空白,而gets
不会空白:( 我的输入是"123 456 789"
)
void string(char str[]){
printf("Enter a string:");
scanf("%s",str);
printf("%s\n", str); // the string was scanned until 1st whitespace
gets(str);
printf("%s\n", str); // proper string was read
}
要注意的第二件事是警告。 strlen()
的返回类型不是int
,而是size_t
( man )。 另外,您正在比较不匹配的类型(char!= int):
void words(char str[]){
int i=0; // change to size_t because it is checked against len
int len = strlen(str); // change to size_t
while( str[i]!=len) // should be i != len because you check the location in the string against the length, not against the char itself!!
{
if(str[i]==' ')
db++;
i++;
}
}
第三件事是逻辑。 如果您的字符串确实有两个单词:“ hello world”,该怎么办? 您只计算white_spaces并假设您只有一个字。 我会添加类似的内容:
空词(char str []){size_t i = 0; size_t len = strlen(str); printf(“%s \\ n”,str);
while(i!=len)
{
if(str[i]==' ')
db++;
i++;
}
if(i) // if i is positive, there must have been a white_space -> add the 1st word because it wasn't counted (Assuming the string must start with a word and not a white_space!!)
i++;
}
这是我将其重写的方式:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int db =0;
char array[255];
void string(char str[]){
printf("Enter a string:");
gets(str);
}
void words(char str[]){
size_t i = 0;
size_t len = strlen(str);
while(i != len)
{
if(str[i]==' ')
db++;
i++;
}
if(i)
i++;
}
int main(/*no need for argc, argv. You can leave them there, but I prefer to omit if not used*/){
string(array);
words(array);
printf("its contain %d words",db);
return 0;
}
请注意,仍然存在一些问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.