c - 如何扫描字符串直到出现特定单词
问题描述
输入:我想成为某种东西 END。END 是那个特定的词。我需要存储我所有的话。
do
{
scanf("%s", row[p]);
p++;
}while(strcmp(niz,'END')!=0);
这是正确的方法吗?
解决方案
如果我正确理解了您的问题,那么您需要以下内容。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char * string_toupper( char *s )
{
for ( char *p = s; *p; ++p ) *p = toupper( ( unsigned char )*p );
return s;
}
int main( void )
{
enum { N = 50 };
char word[N];
char tmp[N];
const char *s = "one two three four end five";
for ( int offset = 0, pos = 0;
sscanf( s + offset, "%s%n", word, &pos ) == 1 && strcmp( string_toupper( strcpy( tmp, word ) ), "END" ) != 0;
offset += pos )
{
puts( word );
}
}
程序输出为
one
two
three
four
或类似以下内容
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char * string_toupper( char *s )
{
for ( char *p = s; *p; ++p ) *p = toupper( ( unsigned char )*p );
return s;
}
int main( void )
{
enum { N = 50 };
char word[N];
for ( char tmp[N]; scanf( "%s", word ) == 1 && strcmp( string_toupper( strcpy( tmp, word ) ), "END" ) != 0; )
{
puts( word );
}
}
如果要进入
one two three four end
那么输出将是
one
two
three
four
推荐阅读
- python - 从批处理文件运行时,模块“urllib”没有属性“请求”错误,但在 IDE 中工作正常
- rest-assured - 如何使用 Rest Assured 从列表中获取值?
- vb.net - 使用循环允许我在控制台应用程序中输入多个数字,然后进行计算
- centos - 如何在 Centos 8 上将 Clamd 作为服务运行
- shopify - 将 js 变量与 Shopify 液体标签一起使用
- python - argParse 的新手,不确定我的错误在哪里
- java - 如何使用哈希图计算成对数字的绝对差?
- html - 如何使用 HTML/CSS 和 Bootstrap 4 使视频适合网页背景?
- git - 删除 repo 1 的子文件夹以创建新的 repo 2 (bitbucket repos)
- java - 如何在 Spark Java 中创建复杂的 StructType Schema