字符串翻转集合, case1, hello world->world hello; case2, hello world->olleh dlrow

Case1: 从hello world 翻转成 world hello


/*大概的理念是先把所有字符串都翻转了,所以hello world 编程dlrow olleh. 然后再每个字符之间翻转,变成world hello(以空位为分界点)*/



Case2: 从hello world翻转成olleh dlrow


/*大概的理念是以空格为分界点,每个字符串之间翻转*/


void reverseWords(char str[])

{

if (!str) return;

int cur=0, start =0;

while(str[cur])

{

if(str[cur]==SPECIAL_CHAR||str[cur]== "  ")

{

cur=cur-1;

reserver(str, start, cur);

start=cur+1;

}

cur++;

}

}

void reverse(char *str, int beg, int end)

{

while(beg

{

char temp=str[beg];

str[beg]=str[end];

str[end]=temp;

beg++;

end--;

}

}

你可能感兴趣的:(面试题)