有关如何阅读最后一个“单词”的提示在 C 的字符数组中

发布于 2024-09-28 19:08:22 字数 421 浏览 7 评论 0原文

只是希望指出正确的方向:

对 C 程序进行标准输入,我一次获取每一行并将其存储在 char[] 中。

现在我有了 char[],如何获取最后一个单词(假设用空格分隔)然后转换为小写?

我已经尝试过这个,但它只是挂起程序:

while (sscanf(line, "%s", word) == 1)
    printf("%s\n", word);

采取建议并提出这个,是否有更有效的方法来做到这一点?

char* last = strrchr(line, ' ')+1;

while (*last != '\0'){   
    *last = tolower(*last);
    putchar((int)*last);
    last++;
}

Just looking to be pointed in the right direction:

Have standard input to a C program, I've taken each line in at a time and storing in a char[].

Now that I have the char[], how do I take the last word (just assuming separated by a space) and then convert to lowercase?

I've tried this but it just hangs the program:

while (sscanf(line, "%s", word) == 1)
    printf("%s\n", word);

Taken what was suggested and came up with this, is there a more efficient way of doing this?

char* last = strrchr(line, ' ')+1;

while (*last != '\0'){   
    *last = tolower(*last);
    putchar((int)*last);
    last++;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

べ映画 2024-10-05 19:08:22

如果我必须这样做,我可能会从 strrchr 开始。这应该能让你看到最后一个词的开头。从那里开始,只需遍历字符并将其转换为小写即可。哦,有一个小细节,您必须首先删除所有尾随空格字符。

If I had to do this, I'd probably start with strrchr. That should get you the beginning of the last word. From there it's a simple matter of walking through characters and converting to lower case. Oh, there is the minor detail that you'd have to delete any trailing space characters first.

没有心的人 2024-10-05 19:08:22

您的代码的问题在于它会重复将句子的第一个单词读入单词。每次调用时它都不会移动到下一个单词。因此,如果您将其作为代码:

char * line = "this is a line of text";

那么每次调用 sscanf 时,它都会将“this”加载到 word 中。由于每次读取 1 个单词,sscanf 将始终返回 1。

The issue with your code is that it will repeatedly read the first word of the sentence into word. It will not move to the next word each time you call it. So if you have this as your code:

char * line = "this is a line of text";

Then every single time sscanf is called, it will load "this" into word. And since it read 1 word each time, sscanf will always return 1.

八巷 2024-10-05 19:08:22

这将有助于:

char dest[10], source [] = "blah blah blah!" ;
int sum = 0 , index =0 ;
while(sscanf(source+(sum+=index),"%s%n",dest,&index)!=-1);
printf("%s\n",dest) ;

This will help:

char dest[10], source [] = "blah blah blah!" ;
int sum = 0 , index =0 ;
while(sscanf(source+(sum+=index),"%s%n",dest,&index)!=-1);
printf("%s\n",dest) ;
夏九 2024-10-05 19:08:22

'strtok' 将根据某些分隔符分割输入字符串,在您的情况下,分隔符将是一个空格,因此它将返回一个“单词”数组,您只需获取最后一个即可。

http://www.cplusplus.com/reference/clibrary/cstring/strtok/

人们可以说明执行此操作的许多不同方法,然后确定哪一种包含最佳性能和可用性特征,或者每种方法的优点和缺点,我只是想用代码片段来说明我上面提到的内容。

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

int main()
{
    char line[] = "This is a sentence with a last WoRd ";

    char *lastWord = NULL;
    char *token = strtok(line, " ");
    while (token != NULL)
    {
        lastWord = token;
        token = strtok(NULL, " ");      
    }

    while (*lastWord)
    {
        printf("%c", tolower(*lastWord++));
    }

    _getch();
}

'strtok' will split the input string based on certain delimitors, in your case the delimitor would be a space, thus it will return an array of "words" and you would simply take the last one.

http://www.cplusplus.com/reference/clibrary/cstring/strtok/

One could illustrate many different methods of performing this operation and then determine which one contained the best performance and useability characteristics, or the advantages and disadvantages of each, I simply wanted to illustrate what I mentioned above with a code snippet.

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

int main()
{
    char line[] = "This is a sentence with a last WoRd ";

    char *lastWord = NULL;
    char *token = strtok(line, " ");
    while (token != NULL)
    {
        lastWord = token;
        token = strtok(NULL, " ");      
    }

    while (*lastWord)
    {
        printf("%c", tolower(*lastWord++));
    }

    _getch();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文