2016-08-17 47 views
-9

我試圖寫一個函數取一個字符串,並返回大寫每個單詞的第一個字母。返回每個單詞的首字母大寫

例如: '在天空中的太陽'=> 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

'如果(我== S [0]'那是什麼呢? – John3136

+1

什麼是'我== S [0]'應該測試? – Blorgbeard

+1

什麼是'string'和'的GetString()'? – MikeCAT

回答

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; 
} 
+0

感謝快速相當通過的答案,我需要一些時間和大量的谷歌搜索到完全明白是怎麼回事的。: ) – cerealCode

+0

谷歌提示:*三元運算符*是用在'(i == 0? last_character:s [i-1])' – MikeCAT

+0

感謝提示,實際上三元運算符可能是我得到的東西之一,我來自Ruby。 – cerealCode

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; 
} 
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; 
} 
0

很坦率地說。

//First the includes 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 
#include <cs50.h> 

int main(void) { 
     string str = GetString(); //Here you should enter your string 
     str[0] = toupper(str[0]); //Capitalize the first letter so you don't have to do extra checking in the for loop 
     for (int i = 1; i < strlen(str); i++){ //Start from the second letter(or space) since index 0 is already capitalized 
       if(str[i-1] == ' ') //Check if the previous character was a space 
         str[i] = toupper(str[i]); //Convert to uppercase 
     } 
     puts(str); //Print out the string, same thing as printf(str); 
} 

我希望我幫助:)

相關問題