【发布时间】:2013-03-30 23:50:39
【问题描述】:
我正在尝试制作一个简单的程序,将用户输入的句子存储在最多 80 个字符的数组中,然后将句子中的每个单词大写。我想我是在正确的轨道上,但是当我运行程序时,只显示句子的第一个单词。
#include<stdio.h>
int main(void)
{
int i; /*indexing integer*/
char sen[81]; /*specify total of 80 elements*/
printf("Enter a sentence no longer than 80 characters: \n\n");
scanf("%s",sen);
if((sen[0]>='a') && (sen[0]<='z'))
{
sen[0]=(char)(sen[0]-32); /*-32 in ascii to capitalize*/
}
for(i=0;sen[i]!='\0';i++) /*loop until null character*/
{
if(sen[i]==' ') /*if space: next letter in capitalized*/
{
if((sen[i+1]>='a') && (sen[i+1]<='z'))
sen[i+1]=(char)(sen[i+1]-32);
}
}
printf("%s",sen);
return 0;
}
我感觉它只打印数组中的第一个单词,因为 scanf 之后的第一个 if 语句,但我不完全确定。任何帮助,将不胜感激。谢谢。
【问题讨论】: