试试下面的代码:)
#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;
}
这是因为你正在改变所有字符为大写:'for(i = 0;我
m0skit0
2014-12-03 16:50:27
[C中每个单词的第一个字母的简单大写]的可能重复(http://stackoverflow.com/questions/20038297/simple-capitalization-of-first-letter-of-each-word-in-c) – b4hand 2014-12-03 17:54:27