C语言分离单词问题(倒置)

来源:互联网 发布:alien skin bokeh mac 编辑:程序博客网 时间:2024/05/16 15:07

分离单词

Time Limit: 3000ms, Memory Limit: 10000KB , Accepted:2473, Total Submissions: 4866

Description

编写程序以字符串为单位,以空格或标点符号(字符串中仅含英文逗号','或小数点'.'作为标点符号)作为分隔符,对字符串中所有单词进行倒排,然后把已处理的字符串(应不含标点符号)打印出来。

Input

输入一个字符串(包含大小写字母、空格、逗号或小数点)

Output

输出处理后的字符串。

  • Sample Input 
    I am a student. I like study.
  • Sample Output

    study like I student a am I



#include<stdio.h>
#include<string.h>
int main()
{
int i,j,k,n;
char s[1000],str1[1000],str2[1000];
gets(s);
n=strlen(s);
j=0;
for(i=n-1;i>=0;i--)
{
str1[j]=s[i];
j++;
}
j=0;
for(i=0;i<n;i++)
    {
        if((str1[i]>=65&&str1[i]<=90)||(str1[i]>=97&&str1[i]<=122))//判断是否为空格,若不是,将该处字符储存到字符串数组中
        {
            str2[j]=str1[i];
            j++; 
        }    
        if((str1[i]<65||(str1[i]>90&&str1[i]<97)||str1[i]>122)&&(str1[i-1]>=65&&str1[i-1]<=90||str1[i-1]>=97&&str1[i-1]<=122)||i==n-1)//当该单词结束时,将储存到字符串数组中的单词倒序输出
        {
            for(k=j-1;k>=0;k--)
            {
            printf("%c",str2[k]);
            }
            j=0;
        }    
        if(str1[i]==' '||str1[i]==','||str1[i]=='.')//当遇到空格时,照常输出
        printf(" ");
        if(i==n-1)printf("\n"); //句尾输出换行符
    }
    return 0;

阅读全文
1 0