2014-12-03 173 views
1

我有一個C源代碼,但是我遇到了問題。我想將字符串中的第一個字母從小寫字母轉換爲大寫字母,但它將所有字母都改爲大寫字母。你能幫我解決這個問題嗎?C源代碼將字符串中的第一個字母從小寫字母改爲大寫字母

#include <stdio.h> 
#include <ctype.h> 
#include <conio.h> 
void main() 
{ 
    char sentence[100]; 
    int count, ch, i; 
    int str[32]; 
    printf("Enter a sentence \n"); 
    for (i = 0; (sentence[i] = getchar()) != '\n'; i++) 
    { 
     ; 
    } 
    sentence[i] = '\0'; 
    /* shows the number of chars accepted in a sentence */ 
    count = i; 
    printf("The given sentence is : %s", sentence); 
    printf("\n Case changed sentence is: "); 
    for (i = 0; i < count; i++) 
    { 
     ch = islower(sentence[i])? toupper(sentence[i]) : tolower(sentence[i]); 
     putchar(ch); 
    } 
    getch(); 
} 

例如

輸入:歡迎謝里夫大學

所需的輸出:歡迎謝里夫大學

實際輸出:歡迎謝里夫大學

+1

這是因爲你正在改變所有字符爲大寫:'for(i = 0;我 m0skit0 2014-12-03 16:50:27

+0

[C中每個單詞的第一個字母的簡單大寫]的可能重複(http://stackoverflow.com/questions/20038297/simple-capitalization-of-first-letter-of-each-word-in-c) – b4hand 2014-12-03 17:54:27

回答

2

您必須檢查當前的字符是一個空格,然後只在空格之後的字符上使用toupper

+0

它看起來更多的評論比答案..請張貼示例代碼,所以它有幫助! – 2014-12-03 16:53:33

+0

@LeBarbu在一般情況下,在一個空格或製表符後面可能會有另一個空格。:) – 2014-12-03 17:46:07

+0

我很樂意發表評論,但我必須擁有50點聲望才能這樣做。 @ Vlad-from-moscow是的,當然,但我只是回答了問題,這似乎是一個學校作業,我認爲給出所有答案有點像幫助作弊 – LeBarbu 2014-12-09 11:05:48

0
ch = ' '; 
for (i = 0; i < count; i++) 
{ 
    ch = isspace(ch) ? toupper(sentence[i]) : tolower(sentence[i]); 
    putchar(ch); 
} 
-1

您需要檢查前面有空格的字符和大寫字母。您還需要檢查第一個特殊情況的字符,因爲它之前沒有空格。

#include <string.h> 
#include <stdio.h> 

int main (void) 
{ 
    char str[] = "this is a test string"; 
    int loop; 

    for (loop=-1; loop<(int) strlen(str)-1; loop++) 
    { 
     // Possible upper case required? 
     if (loop < 0 || str[loop]==' ') 
      if (str[loop+1] >= 'a' && str[loop+1] <='z') 
       str[loop+1] = (str[loop+1] - 'a') + 'A'; 
    } 

    printf ("string is : %s\n", str); 

    return 0; 
} 

輸出:

string is : This Is A Test String 
+0

@downvoter關心評論? – Chris 2014-12-03 17:58:49

+0

我覺得'''應該是'&&'。 – BLUEPIXY 2014-12-04 18:04:43

+0

Na。如果是&&循環將不得不<0,因此訪問該陣列將是非法的。 -1代表上部封套的第一個字母 – Chris 2014-12-04 20:18:26

-1

試試下面的代碼:)

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

#define N 100 

int main(void) 
{ 
    char sentence[N]; 
    char *p = sentence; 

    printf("Enter a sentence: "); 

    if (!fgets(sentence, sizeof(sentence), stdin)) sentence[0] = '\0'; 

    printf("\nThe given sentence is : %s", sentence); 

    do 
    { 
     while (isblank((unsigned char)*p)) ++p; 
     if (islower((unsigned char)*p )) *p = toupper(*p); 
     while (*p && !isblank((unsigned char)*p)) ++p; 
    } while (*p); 

    printf("\nCase changed sentence is: %s", sentence); 

    return 0; 
} 

輸出是

The given sentence is : welcome to Sharif university 

Case changed sentence is: Welcome To Sharif University 

如果侑編譯器不支持功能isblank那麼你。可以代替itute它isspace

似乎更正確的方法將只有isalpha使用,因爲在一般情況下,後一個空白可以有例如一個數字或標點符號

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

#define N 100 

int main(void) 
{ 
    char sentence[N]; 
    char *p = sentence; 

    printf("Enter a sentence: "); 

    if (!fgets(sentence, sizeof(sentence), stdin)) sentence[0] = '\0'; 

    printf("\nThe given sentence is : %s", sentence); 

    do 
    { 
     while (*p && !isalpha((unsigned char)*p)) ++p; 
     if (islower((unsigned char)*p )) *p = toupper(*p); 
     while (isalpha((unsigned char)*p)) ++p; 
    } while (*p); 

    printf("\nCase changed sentence is: %s", sentence); 

    return 0; 
} 

如果你不想更改原始字符串,然後代碼將看起來像

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

#define N 100 

int main(void) 
{ 
    char sentence[N]; 
    char *p = sentence; 

    printf("Enter a sentence: "); 

    if (!fgets(sentence, sizeof(sentence), stdin)) sentence[0] = '\0'; 

    printf("\nThe given sentence is : %s", sentence); 

    printf("\nCase changed sentence is: "); 
    do 
    { 
     while (*p && !isalpha((unsigned char)*p)) putchar(*p++); 
     if (islower((unsigned char)*p )) putchar(toupper(*p++)); 
     while (isalpha((unsigned char)*p)) putchar(*p++); 
    } while (*p); 


    return 0; 
} 
相關問題