简体   繁体   English

将字符串中每个单词的首字母大写并预先形成单词计数

[英]Capitalizing the first letter of every word in a string and preforming a word count

Beginner here learning C. I need to input a paragraph of text and then write two functions. 初学者在这里学习C.我需要输入一段文字,然后写两个函数。

the first function capitalizes every first letter of the text. 第一个函数将文本的每个首字母大写。 (This Is An Example) (这是一个例子)

the second function needs to count all the words in the paragraph. 第二个函数需要计算段落中的所有单词。

First step I am taking is inputing the paragraph, I started by just adding the first two sentences. 我正在采取的第一步是输入段落,我开始只是添加前两个句子。 How do I format this paragraph into this string without writing it all on one line? 如何将此段落格式化为此字符串而不将其全部写在一行上? Any help is appreciated. 任何帮助表示赞赏。 Thanks 谢谢

int main (void)
{

char prose[] = "She should have died hereafter;
                There would have been a time for such a word.";

printf("%s\n",prose);




return 0;
}

This is the while loop I am trying to use to detect where the non alphabetic symbols are. 这是我试图用来检测非字母符号所在的while循环。

while (prose[i])
{
    if (isalpha(prose[i]))
        printf("%c is alpha\n",prose[i]);
             else

                 printf("%c is not alpha\n",prose[i]);
                i++;

        }

Any help on where to move from here? 有关从哪里搬到这里的任何帮助?

Adjacent string literals in C are concatenated into a single string, so you can easily split it across lines like this: C中相邻的字符串文字连接成一个字符串,因此您可以轻松地将其拆分为以下行:

char prose[] = "She should have died hereafter;\n"
               "There would have been a time for such a word.";

(Eventually you might want to read the prose from a file or perhaps stdin , instead of hard-coding it into your program.) (最后你可能想从文件或stdin读取散文,而不是将其硬编码到你的程序中。)

To capitalize letters, iterate through the string one character at a time and capitalize every alphabet letter if the previous character was not an alphabet letter (or whatever your criterion is). 要大写字母,一次遍历字符串一个字符,如果前一个字符不是字母(或任何标准),则将每个字母大写。 See ctype.h for helpful functions. 有关有用的功能,请参阅ctype.h

For counting words, you can do something similar, but instead of modifying the string you just increment a counter every time you hit a new word. 对于单词计数,你可以做类似的事情,但不是修改字符串,而是每次点击一个新单词时只需增加一个计数器。

In both cases check that your code handles the first and last words correctly. 在这两种情况下,检查您的代码是否正确处理了第一个和最后一个字

I guess you want this: 我想你想要这个:

    char prose[] = "\
She should have died hereafter.\n\
There would have been a time for such a word.\n\
";
    printf("%s\n",prose);

\\n is for an actual new line in the string, \\ at the end of line lets the statement continue on the next line. \\n是针对字符串中的实际新行, \\在行尾让该语句在下一行继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM