70,037
社区成员
发帖
与我相关
我的任务
分享
/*编写一个函数jsChange(),实现一个英文句子的改写功能,并给出测试程序。
改写条件:扫描句子,以空格或标点符号为分隔符,对句子中所有单词进行倒排,
单词间以空格作为分隔符。*/
#include "stdio.h"
#include "string.h"
char *js_change(char arry[]); /*js_change()函数声明*/
main()
{
char arry[100];
char *str = gets(arry);
str = js_change(arry);
puts(str);
printf("\n");
}
char *js_change(char arry[])
{
int len = strlen(arry);
char * head = (char *)malloc((len)*sizeof(char)); /*动态创建一个用于存放倒排的数组*/
char *end = arry+(len-2); /*尾指针指向最后标点符号的前一位*/
char * temp = NULL; /*中间指针变量*/
char * Cursor = arry+len-1; /*游标*/
int i=0; /*控制循环变量*/
*(head+len) = '\0'; /*为动态数组添加结束符*/
while(i < len)
{
Cursor--;
while(' ' != *Cursor && Cursor >= arry && ',' != *Cursor) /*遍历每一个单词*/
Cursor--;
temp = Cursor; /*记住已遍历的位置*/
while(Cursor < end) /*把遍历到的单词写到创建的动态数组里面*/
{
*(head+i) = *(Cursor+1);
i++;
Cursor++;
}
*(head+i) = ' '; /*空格做为每个单词的分隔符*/
i++;
end = temp-1; /*去掉已遍历到的单词,重置尾指针*/
Cursor = temp; /*恢复已遍历到的位置*/
}
return head; /*返回倒排的句子的首指针*/
}
/*******************************************************************************************/
/*1、 编写一个函数jsSort(),实现一个英文句子排序的功能,并给出测试程序。排序条件:
从字符串中间一分为二,左边部分按字符的ASCII值降序排序,右边部分则升序排序,
如果字符串长度为奇数,则中间字符不参加排序,字符仍放在原位置上。*/
#include "stdio.h"
#include "string.h"
jsSort(char arry[]);
main()
{
char arry[100];
printf("Please enter a sentence:");
gets(arry); /*输入英文句子*/
jsSort(arry); /*调用jsSort函数*/
puts(arry);
printf("\n");
}
jsSort(char arry[])
{
int i = strlen(arry);
char *font = &arry[0]; /*指向句子头的指针*/
char *end = &arry[i-1]; /*指向句子尾的指针*/
char *mind = &arry[(i-1)/2]; /*定义一个指向句子中间的指针*/
char temp; /*用于交换的中间变量*/
char *cursor; /*排序用到的游标*/
/************判断字符串长度奇偶,若为奇数*************/
if(0 != i%2)
{
/*********左半边排序*********/
while(font != mind)
{
cursor=font;
while(cursor != mind)
{
if(*cursor > *font)
{
temp = *cursor;
*cursor = *font;
*font = temp;
}
cursor++;
}
font++;
}
/*********右半边排序********/
font = mind+1;
while(font <= end)
{
cursor=font;
while(cursor <= end)
{
if(*cursor < *font)
{
temp = *cursor;
*cursor = *font;
*font = temp;
}
cursor++;
}
font++;
}
}
/*************若句子长度为偶数*************/
else
{
/************左半边排序**********/
while(font <= mind)
{
cursor=font;
while(cursor <= mind)
{
if(*cursor > *font)
{
temp = *cursor;
*cursor = *font;
*font = temp;
}
cursor++;
}
font++;
}
/***********右半边排序**********/
font = mind+1;
while(font <= end)
{
cursor=font;
while(cursor <= end)
{
if(*cursor < *font)
{
temp = *cursor;
*cursor = *font;
*font = temp;
}
cursor++;
}
font++;
}
}
/* free(font);
free(end);
free(mind);
free(cursor);
*/
}
/***************************************************************************/
/*2、 编写一个函数jsChange(),实现一个英文句子的改写功能,并给出测试程序。
改写条件:扫描句子中的小写字母o,每次将小写字母o的左右字符串部分交叉换位,
即左边字符串移到o的右边,右边字符串反之,并把o删除,
依次直至这一行中的小写字母o处理完。*/
#include "stdio.h"
#include "string.h"
char *jsChange(char arry[]);
main()
{
char arry[100];
char *Str = gets(arry);
Str = jsChange(Str);
puts(Str);
printf("\n");
}
char *jsChange(char *p)
{
char *font = p;
char *head = p;
char *cursor = NULL;
char *temp = NULL;
while('\0' != *font)
{
/*方法一(效率较低)
for(cursor = font;'\0' != *cursor;cursor++)
{
if('o' == *cursor)
{
temp = cursor;
cursor++;
*temp = '\0';
strncat(cursor,head,strlen(head));
head = cursor;
font = head;
break;
}
}*/
/*方法二*/
if('o' == *font)
{
temp = font;
font++;
*temp = '\0';
strncat(font,head,strlen(head));
head = font;
}
font++;
}
return head;
}