C语言句子字母排序后行输出

来源:百度知道 编辑:UC知道 时间:2024/06/14 05:25:29
C语言的一个程序,要求输入一个句子,然后程序先将句子中的每个单词的首字母进行从A到Z排序,最后将排序后输出结果为每行一个单词。程序只能识别字母或空格。不能用 <string.h>,用到要自己写。

例如
输入:I love China

输出:
China

I

love

代码应该怎么写呢?请教。谢谢

#include <iostream>
using namespace std;

void main()
{
char ch[100];
cin.getline(ch,100);
cout<<ch<<endl;

int dex=0;
while (ch[dex]!='\0')
{
if ((ch[dex]>'Z'||ch[dex]<'A')&&(ch[dex]>'z'||ch[dex]<'a')&&ch[dex]!=' ')
{
cout<<"ERROR at: "<<ch+dex<<endl;
return ;
}
dex++;
}

for (char c='a';c<='z';c+=1)
{
dex=0;
while (ch[dex]==' ')
{
dex++;
}

while (ch[dex]!='\0')
{
if (ch[dex]==c || ch[dex]==c-('a'-'A'))
{
cout<<"输出单词:";
while (ch[dex]!=' ' && ch[dex]!='\0')
{
cout<<ch[dex];
dex++;
}
cout<<endl;
}