2014-12-03 122 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; 
} 
相关问题