gpt4 book ai didi

c - C语言中如何判断一个单词是否在行尾

转载 作者:行者123 更新时间:2023-11-30 19:26:02 25 4
gpt4 key购买 nike

所以我需要解决一个问题,我必须从文件中读取单词,并且仍然能够识别单词是否位于行尾。例如:

This is a test file
I need to distinguish every word
and still be able to print out every words that are at the end of a line

我尝试使用:

while(scanf("%s", string) != EOF){
len = strlen(string);
if (string[len] == '\n'){
printf("%s is a word at the end of a line", string);
}
}

但似乎没有什么能正常工作。有人可以帮助我吗?

编辑1:我已经尝试过

if (string[len-1] == '\n')

但它给出了单词的最后一个字符。例如,“file”的单词 string[len-1] 是“e”,即使它是第 1 行末尾的单词

最佳答案

您的 scanf 调用读取单词,每次返回一个。它会跳过空格和换行符,因此您无法查看其中一个单词来确定它是否位于行尾。您需要一种不同的方法。

关于c - C语言中如何判断一个单词是否在行尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58038197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com