C语言 数组 输入一行文字,要求将每个单词的首字母由小写改为大写,单词间用空格隔开

如题所述

#include"stdio.h"
void main()
{
int i;
char str[100];
printf("请输入一个字符串:");
gets(str);
if(str[0]>=97&&str[0]<=122)
str[0]=str[0]-32;
for(i=1;str[i]!='\0';i++)
{if(str[i-1]==' '&&str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
puts(str);
}

 

希望我的回答会对您有所帮助!

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-06-17
#include<stdio.h>
#define N 100
void main()
{
    char a[N]={"what are you talking about?"};
    int i;
    for(i=0;i<N;i++)
        if(i>0&&a[i-1]==' ')a[i]-=32;
        else if(i==0)a[i]-=32;
    printf("%s",a);
}
//已测试,输出结果为What Are You Talking About?
//因为不知道具体句子是怎么样的,我就按字母都是小写来处理了

相似回答