c语言编写程序颠倒句子
来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/19 20:20:51
c语言编写程序颠倒句子
enter a sentence:you can case a swallow can't you?
reversal of sentence:you can't swallow a cage can you?
enter a sentence:you can case a swallow can't you?
reversal of sentence:you can't swallow a cage can you?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<ctype.h>
#define OK 1
#define M 5
//建单词节点
struct Node{
\x09char *data;
\x09Node *next;
};
char f;
//插入函数
void Insert(Node* &S,char e[])
{
\x09Node *p;
\x09p=(Node *)malloc(sizeof(Node));
\x09p->data = (char *)malloc(strlen(e)+1);
\x09p->next = NULL;
strcpy(p->data,e);
\x09p->next = S;
\x09S = p;
}
void Creat(Node* &S)
{
\x09int i = 0,k = 0,j,flag = 1,insert;
\x09char s[20];
\x09char a[2000] = {0};
printf("\n输入正文: ");
\x09fflush(stdin);
\x09gets(a);
printf("\n原文为: ");
\x09while(a[i] != NULL)
\x09{
\x09\x09insert = 0;
\x09\x09memset(s, 0, 20);
\x09\x09j = 0;
\x09\x09// 跳过单词前面的前导字符
\x09\x09while(flag&&!isalpha(a[i])||a[i]==39)//39为'的ASCII码
\x09\x09{
\x09\x09\x09i++;
\x09\x09\x09if(a[i]==NULL)
\x09\x09\x09\x09flag = 0;
\x09\x09}
\x09\x09// 当前单词尚未结束,则一直循环
\x09\x09while(flag&&isalpha(a[i])||a[i]==39)
\x09\x09{
\x09\x09\x09insert = 1;
\x09\x09\x09s[j]=tolower(a[i]);
\x09\x09\x09i++;
\x09\x09\x09j++;
\x09\x09}
\x09\x09//单词插入
\x09\x09if(insert)
\x09\x09{
\x09\x09\x09s[i] = '\0';
\x09\x09\x09k++;
\x09\x09\x09printf("%s\t", s);
\x09\x09\x09if(k%5 == 0)
\x09\x09\x09\x09printf("\n");
\x09\x09\x09Insert(S,s);
\x09\x09\x09j++;
\x09\x09}
\x09}
\x09if(!isalpha(a[i-1]))
\x09\x09f = a[i-1];
\x09else
\x09\x09f=' ';
}
//输出正文
void Print(Node* &T)
{
if(NULL!=T)
{
\x09\x09printf("%s ",T->data);
\x09\x09Print(T->next);
\x09}
\x09else
\x09\x09return;
}
int main()
{
Node *S=NULL;
\x09Creat(S);
\x09printf("\n倒置为:");
\x09Print(S);
\x09printf("%c",f);
\x09printf("\n");
return 0;
}
2013 5 25 8:16耗时14min1L
#include <stdlib.h>
#include <string.h>
#include<ctype.h>
#define OK 1
#define M 5
//建单词节点
struct Node{
\x09char *data;
\x09Node *next;
};
char f;
//插入函数
void Insert(Node* &S,char e[])
{
\x09Node *p;
\x09p=(Node *)malloc(sizeof(Node));
\x09p->data = (char *)malloc(strlen(e)+1);
\x09p->next = NULL;
strcpy(p->data,e);
\x09p->next = S;
\x09S = p;
}
void Creat(Node* &S)
{
\x09int i = 0,k = 0,j,flag = 1,insert;
\x09char s[20];
\x09char a[2000] = {0};
printf("\n输入正文: ");
\x09fflush(stdin);
\x09gets(a);
printf("\n原文为: ");
\x09while(a[i] != NULL)
\x09{
\x09\x09insert = 0;
\x09\x09memset(s, 0, 20);
\x09\x09j = 0;
\x09\x09// 跳过单词前面的前导字符
\x09\x09while(flag&&!isalpha(a[i])||a[i]==39)//39为'的ASCII码
\x09\x09{
\x09\x09\x09i++;
\x09\x09\x09if(a[i]==NULL)
\x09\x09\x09\x09flag = 0;
\x09\x09}
\x09\x09// 当前单词尚未结束,则一直循环
\x09\x09while(flag&&isalpha(a[i])||a[i]==39)
\x09\x09{
\x09\x09\x09insert = 1;
\x09\x09\x09s[j]=tolower(a[i]);
\x09\x09\x09i++;
\x09\x09\x09j++;
\x09\x09}
\x09\x09//单词插入
\x09\x09if(insert)
\x09\x09{
\x09\x09\x09s[i] = '\0';
\x09\x09\x09k++;
\x09\x09\x09printf("%s\t", s);
\x09\x09\x09if(k%5 == 0)
\x09\x09\x09\x09printf("\n");
\x09\x09\x09Insert(S,s);
\x09\x09\x09j++;
\x09\x09}
\x09}
\x09if(!isalpha(a[i-1]))
\x09\x09f = a[i-1];
\x09else
\x09\x09f=' ';
}
//输出正文
void Print(Node* &T)
{
if(NULL!=T)
{
\x09\x09printf("%s ",T->data);
\x09\x09Print(T->next);
\x09}
\x09else
\x09\x09return;
}
int main()
{
Node *S=NULL;
\x09Creat(S);
\x09printf("\n倒置为:");
\x09Print(S);
\x09printf("%c",f);
\x09printf("\n");
return 0;
}
2013 5 25 8:16耗时14min1L