c语言设计程序

来源:百度知道 编辑:UC知道 时间:2024/05/13 05:38:33
设计函数str()的功能是输入一个英文句子(字符串),将其中每个单词的最后一个字母改写成大写,然后输出此句子。(这里的“单词”是指由空格格开的字符串)。例如:若输入:“this is a book“,则应输出“thiS iS A booK".

#include <stdio.h>
#include <ctype.h>
#include <string.h>
void str()
{
int i = 0;
char str[255];
fgets(str, sizeof(str), stdin);
if(str[0]) //第一个字符不为结束符
{
while(str[i++])
{
if(isspace(str[i])) //若str[i]为空格,制表等字符
{
str[i-1] = toupper(str[i-1]); //转化为大写
}
}
}
}

不对,例如:this is a book,this和is之间有多个空格时,会出错吧

正解