返回每个单词的首字母大写

时间:2016-08-17 02:10:45

标签: c cs50

我试图写一个函数来取一个字符串,然后返回每个单词大写的第一个字母。

例如:'天空中的太阳' => TSITS

这是我的代码。经过修补,我设法能够编译;但似乎只打印字符串的空格

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <cs50.h>   // typedef char *string; string GetString();

int main(void)
{
    string s = GetString();

    for (int i=0;i<strlen(s);i++){
        if(i == s[0] || s[i-1] == ' ' ){
            s[i] = toupper(s[i]);
            printf("%c",i);
            i++;
        }
    }
}

它有什么问题?

4 个答案:

答案 0 :(得分:0)

  • stringGetString被使用,但您没有定义它们,或者您没有透露命令行或定义它们的环境。
  • 条件i == s[0]是无意义的,因为它将索引与第一个字符的字符代码进行比较。它还可能导致调用未定义的行为以进行s[-1]的超出范围访问。
  • 打印语句printf("%c",i);错误,因为它打印索引而不是字符。

尝试使用此代码,该代码处理输入的第一行:

#include <stdio.h>
#include <ctype.h>

int main(void)
{
    char s[1024];
    char last_character = ' '; /* to deal with word lying on multiple chunks */

    /* repeat for the case where the line to deal with is too long to fit in one chunk */
    while (fgets(s, sizeof(s), stdin) != NULL) {
        int i;
        /* calling strlen() in every loop is not effective */
        for (i = 0; s[i] != '\0'; i++){
            /* check if currently looking at the first charater of each words */
            if(isalpha((unsigned char)s[i]) && (i == 0 ? last_character : s[i-1]) == ' '){
                /* cast to unsigned char to avoid undefined behavior invoked by passing out-of-range value */
                printf("%c", toupper((unsigned char)s[i]));
                /* incrementing i here may cause skipping the terminating null-character for the new loop condition, so remove it */
            }
        }
        if (i > 0) {
            /* save the last character in this chunk */
            last_character = s[i - 1];
            /* deal with only the first line */
            if (last_character == '\n') break;
        }
    }
    putchar('\n');
    return 0;
}

答案 1 :(得分:0)

string func(string s){
//take a string, and return the first letter of each word capitalized.
    char pre = ' ', curr;
    int new_i = 0;

    for(int org_i = 0; curr = s[org_i]; ++org_i){
        if(isspace(curr))
            pre = ' ';
        else if(pre == ' ')
            s[new_i++] = toupper(pre = curr);
    }
    s[new_i] = 0;
    return s;
}

int main(void){
    string s = GetString();

    puts(func(s));
    free(s);

    return 0;
}

答案 2 :(得分:0)

而不是使用i == s[0](可能意味着i == 0),只需跟踪前一个字符。

char previous = ' ';

// Don't recalculate the length each time, just look for the null character
// for (int i=0;i<strlen(s);i++){
for (; *s; s++){
    if(previous == ' ' && isalpha((unsigned char) *s)) {
      printf("%c",toupper((unsigned char) *s));
      // or 
      putchar(toupper((unsigned char) *s));
    }
    previous = *s; 
}

答案 3 :(得分:0)

很坦率地说。

....
if(doc.LoadFile("daily.xml") == tinyxml2::XML_SUCCESS)
{
    tinyxml2::XMLElement* root = doc.FirstChildElement();

    for(tinyxml2::XMLElement* elem = root->FirstChildElement("forecast"); elem != NULL; elem = elem->NextSiblingElement())
    {
    ....

我希望我帮助过。)