作业帮 > 综合 > 作业

C语言每个句子的每一个单词 倒置

来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/18 00:57:29
C语言每个句子的每一个单词 倒置
给出一个句子,把每一个字母倒置,但不改变单词的顺序.
输入:
这个题有多个CASE.第一行给出一个正整数N(N
C语言每个句子的每一个单词 倒置
#include
#include
#define MAXBLOCKS 499 // 块数目上限
#define MAXSENSIZE 300 // 句子长度上限
#define INITSENSIZE 600 // 句子池初始容量
#define INCRESENSIZE 400 // 句子池渐增容量
/* 仓库 */
typedef struct
{
int count; // 块数目(count=nSize;
lpStorage->blocksize=(int *)malloc(sizeof(int) * nSize);
lpStorage->sentence=(char *)malloc(sizeof(char) * INITSENSIZE);
lpStorage->sentencesize=INITSENSIZE;
lpStorage->sentenceused=0;
if(!lpStorage->blocksize || !lpStorage->sentence)
{
DestroyStorage(lpStorage);
return 0;
}
else
{
return 1;
}
}
int DestroyStorage(STORAGE *lpStorage)
{
if(!lpStorage)
return 0;
lpStorage->count=0;
lpStorage->sentencesize=0;
lpStorage->sentenceused=0;
free(lpStorage->blocksize);
free(lpStorage->sentence);
lpStorage->blocksize=NULL;
lpStorage->sentence=NULL;
return 1;
}
int EnlargeStorage(STORAGE *lpStorage)
{
char *newptr;
if(!lpStorage)
return 0;
newptr=(char *)realloc(lpStorage->sentence,sizeof(char) \
* (lpStorage->sentencesize+INCRESENSIZE));
if(!newptr)
return 0;
lpStorage->sentence=newptr;
lpStorage->sentencesize +=INCRESENSIZE;
return 1;
}
int InputStorage(STORAGE *lpStorage)
{
int i,j;
char *start,*end;
if(!lpStorage)
return 0;
for(i=0;icount;++i)
{
scanf(" %d",lpStorage->blocksize+i);
if(lpStorage->blocksize[i]sentencesize-lpStorage->sentenceused) < (MAXSENSIZE+2))
{
if(!EnlargeStorage(lpStorage))
return 0;
}
start=fgets(lpStorage->sentence+lpStorage->sentenceused,MAXSENSIZE,stdin);
if(!start)
return 0;
for(end=start;*end;++end)
{
if(*end=='\n')
{
lpStorage->sentenceused +=end-start+1;
*end='\0';
ReverseString(start);
}
}
}
}
return 1;
}
int ReverseString(char *str)
{
char c;
char *start,*end;
if(!str)
return 0;
do
{
start=str;
while(*start==' ' || !*start)
{
if(!*start)
return 1;
++start;
}
end=start;
while(*end!=' ' && *end)
{
++end;
}
str=end;
--end;
while(startsentence;
for(i=0;icount;++i)
{
puts("");
for(j=0;jblocksize[i];++j)
{
puts(str);
while(*str++)
;
}
}
return 1;
}
int StartWork(void)
{
int count;
STORAGE stStorage;
scanf(" %d",&count);
if(countMAXBLOCKS)
return 0;
if(!InitStorage(&stStorage,count))
return 0;
if(!InputStorage(&stStorage))
return 0;
if(!ShowStorage(&stStorage))
return 0;
if(!DestroyStorage(&stStorage))
return 0;
return 1;
}