Chinaunix首页 | 论坛 | 博客
  • 博客访问: 818141
  • 博文数量: 756
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 4980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:40
文章分类

全部博文(756)

文章存档

2011年(1)

2008年(755)

我的朋友

分类:

2008-10-13 16:14:18

看roo的Blog,上面有一篇《 》。
【问 题】
    将一个字符数组中的单词顺序反转,要求不借助其它字符数组或者指针链表,保存反转后的字符数组,单词间以空格分隔。

【思 路】
先将字符数组全部反转,“This is a test”--> “tset a si sihT”, 再将单词逐个反转,最后得到要求的数组。

他的思路很清晰,就是先把一个字符串全部翻转,再逐个反转单词。其实我觉得还有更好的方法,完全可以省掉空间和时间上的浪费,一遍扫过就可以了。代码如下:
//
// Write by Spark
// 2006-04-02
//
#include 

int main(int argc, char *argv[])
{
    char str[] ="This is a test";
    int len = sizeof(str);
    char * p1, * p2, * p3;

    p1 = str + len - 2;   
    while(p1 != str)
    {
        p3 = p1; //remember start position

        // find the space char
        while(*p1 != ' ' && p1 != str)
        {            
            p1--;
        }		

        // output a section
        p2 = (p1 == str ? p1 : p1+1);
        for (;p2!=p3;p2++)
            printf("%c", *p2);
        printf("%c", *p3);
        
        // output the space char
        if (p1==str)
            break;
    	p1--;
        printf(" ");
    }
	
	return 0;
}
-------------
乾坤一笑 写于2006年04月02日  转载请标明出处和原文链接 

--------------------next---------------------
我写的,仅用一次循环(一重循环)
解决倒序问题.而且适用于", this is a test. "
这种类型的字符串.


#include 
#include 
void StringReverse(char *);

int main()
{
char s[]=", this is a test. ";
StringReverse(s);
printf("%s\n",s);
return 0;
}

void StringReverse(char * string)
{
//assert(string!=NULL);
char * StrPtr=string;
char str,* StrPtr2;
int tmp;

while(*StrPtr!='\0') StrPtr++;
for(StrPtr2=StrPtr--,tmp=((*StrPtr>='a'&&*StrPtr<='z')||(*StrPtr>='A'&&*StrPtr<='Z'));
StrPtr>=string; StrPtr--) {
if (tmp!=((*StrPtr>='a'&&*StrPtr<='z')||(*StrPtr>='A'&&*StrPtr<='Z'))) {
tmp=!tmp;
str=*StrPtr2;
*StrPtr2='\0';
printf("%s",StrPtr+1);
*StrPtr2=str;
StrPtr2=StrPtr+1;
}
}
str=*StrPtr2;
*StrPtr2='\0';
printf("%s\n",++StrPtr);
*StrPtr2=str;
}
--------------------next---------------------
#include
#define SWAP(x,y)  {x = x + y; y = x - y; x = x - y;}
#define MAXSIZE 1024


void convert(char *s, int length)
{
int i;
for (i = 0; i < length/2; i++)
{
SWAP(s[i],s[length-i-1])
}
}
void reverse(char *s)
{
int i, j;
char *p;
int character, word;

p = s;
character = 0;
word = 0;
i = strlen(s);
convert(p,i);

for (j = 0; j <= i; j++)
{
//character = 0;
if (s[j] != ' ' && s[j] != '\0')
{
character++;
}
else if (character)
{
convert(&s[j-character],character);
character = 0;
}
}
}

void print(char *c)
{
printf("%s\n",c);
}

int main(void)
{
int i;
char ch[MAXSIZE];
char c;
int lable = 0 ;

i = 0;
printf("enter you character:\n");
while((c =  getchar()) != '\n')
{
if (c != ' ')
{
ch[i++] = c;
lable = 0;
}
else
{
if (!lable)
{
ch[i++] = c;
lable = 1;
}
}
}

ch[i] = '\0';

puts("you first input:   ");
print(ch);
reverse(ch);
puts("you reverse input:   ");
print(ch);

return 0;
}








--------------------next---------------------
#include
#define SWAP(x,y)  {x = x + y; y = x - y; x = x - y;}
#define MAXSIZE 1024


void convert(char *s, int length)
{
int i;
for (i = 0; i < length/2; i++)
{
SWAP(s[i],s[length-i-1])
}
}
void reverse(char *s)
{
int i, j;
char *p;
int character, word;

p = s;
character = 0;
word = 0;
i = strlen(s);
convert(p,i);

for (j = 0; j <= i; j++)
{
//character = 0;
if (s[j] != ' ' && s[j] != '\0')
{
character++;
}
else if (character)
{
convert(&s[j-character],character);
character = 0;
}
}
}

void print(char *c)
{
printf("%s\n",c);
}

int main(void)
{
int i;
char ch[MAXSIZE];
char c;
int lable = 0 ;

i = 0;
printf("enter you character:\n");
while((c =  getchar()) != '\n')
{
if (c != ' ')
{
ch[i++] = c;
lable = 0;
}
else
{
if (!lable)
{
ch[i++] = c;
lable = 1;
}
}
}

ch[i] = '\0';

puts("you first input:   ");
print(ch);
reverse(ch);
puts("you reverse input:   ");
print(ch);

return 0;
}








--------------------next---------------------

阅读(795) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~