读书人

请问:字符串中字符倒序排列不借用另

发布时间: 2013-10-19 20:58:22 作者: rapoo

请教:字符串中字符倒序排列,不借用另一字符串
请教各位达人,如何将字符串中的字符在同一字符串中倒序排列,不借用另一字符串。例如,输入串为“abcd”,则将该串变为”dcba“。这是含有偶数个字符的串,如果是含有奇数个字符的串,可能就又又同了。我做了一个函数,但达不到效果。

void reverse(char *str)
{
int i, j;
char temp;

for(i = 0, j = strlen(str)-1;
i <= strlen(str)/2, j > strlen(str)/2;
i++, j--)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}

麻烦帮忙看看问题出在哪里,谢谢!
[解决办法]
试试这个:
j = strlen(str);
for(i = 0; i <= j / 2; i++)
{
temp = str[i];
str[i] = str[j - i];
str[j - i] = temp;
}
[解决办法]
i <= strlen(str)/2, j > strlen(str)/2
改成
(i <= strlen(str)/2) && (j > strlen(str)/2)
[解决办法]

版本1
void reverse(char *str)
{
int i;
int j = strlen(str);
char temp;
for (i=0; i<j;i++,j--)
{
temp = str[i];
str[i] = str[j-1];
str[j-1] = temp;
}
}



版本2
void reverse(char *str)
{
int i;
int j = strlen(str);
char temp;
for (i=0; i<j/2;i++)
{
temp = str[i];
str[i] = str[j-i-1];
str[j-i-1] = temp;
}
}


版本3
void reverse(char *str)
{
char temp;
char *last_char;

for (last_char = str; *last_char != '\0'; last_char++)
;
last_char--;

while (str < last_char)
{
temp = *str;
*str++ = *last_char;
*last_char-- = temp;
}
}


版本4
void reverse(char *str)
{
char temp;
char *last_char;

for (last_char = str; *last_char != '\0'; last_char++)
;
last_char--;
while(str<last_char)
{
*str = *str ^ *last_char;
*last_char = *str ^ *last_char;
*str = *str ^ *last_char;;
str++;
last_char--;
}
}

[解决办法]
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\intel\strrev.asm
        page    ,132
title strrev - reverse a string in place
;***
;strrev.asm - reverse a string in place
;
; Copyright (c) Microsoft Corporation. All rights reserved.
;
;Purpose:
; defines _strrev() - reverse a string in place (not including
; '\0' character)
;
;*******************************************************************************

.xlist
include cruntime.inc
.list

page
;***
;char *_strrev(string) - reverse a string in place
;
;Purpose:
; Reverses the order of characters in the string. The terminating
; null character remains in place.
;
; Algorithm:
; char *
; _strrev (string)
; char *string;
; {
; char *start = string;
; char *left = string;
; char ch;
;
; while (*string++)
; ;


; string -= 2;
; while (left < string)
; {
; ch = *left;
; *left++ = *string;
; *string-- = ch;
; }
; return(start);
; }
;
; NOTE: There is a check for an empty string in the following code.
; Normally, this would fall out of the "cmp si,di" instruction in the
; loop portion of the routine. However, if the offset of the empty
; string is 0 (as it could be in large model), then the cmp does not
; catch the empty string and the routine essentially hangs (i.e., loops
; moving bytes one at a time FFFFh times). An explicit empty string
; check corrects this.
;
;Entry:
; char *string - string to reverse
;
;Exit:
; returns string - now with reversed characters
;
;Uses:
;
;Exceptions:
;
;*******************************************************************************

CODESEG

public _strrev
_strrev proc \
uses edi esi, \
string:ptr byte

mov edi,[string] ; di = string
mov edx,edi ; dx=pointer to string; save return value

mov esi,edi ; si=pointer to string
xor eax,eax ; search value (null)
or ecx,-1 ; cx = -1
repne scasb ; find null
cmp ecx,-2 ; is string empty? (if offset value is 0, the
je short done ; cmp below will not catch it and we'll hang).

sub edi,2 ; string is not empty, move di pointer back
; di points to last non-null byte

lupe:
cmp esi,edi ; see if pointers have crossed yet
jae short done ; exit when pointers meet (or cross)

mov ah,[esi] ; get front byte...
mov al,[edi] ; and end byte
mov [esi],al ; put end byte in front...


mov [edi],ah ; and front byte at end
add esi,1 ; front moves up...
sub edi,1 ; and end moves down
jmp short lupe ; keep switching bytes

done:
mov eax,edx ; return value: string addr

ret ; _cdecl return

_strrev endp
end


[解决办法]
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\strrev.c
/***
*strrev.c - reverse a string in place
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines _strrev() - reverse a string in place (not including
* '\0' character)
*
*******************************************************************************/

#include <cruntime.h>
#include <string.h>

/***
*char *_strrev(string) - reverse a string in place
*
*Purpose:
* Reverses the order of characters in the string. The terminating
* null character remains in place.
*
*Entry:
* char *string - string to reverse
*
*Exit:
* returns string - now with reversed characters
*
*Exceptions:
*
*******************************************************************************/

char * __cdecl _strrev (
char * string
)
{
char *start = string;
char *left = string;
char ch;

while (*string++) /* find end of string */
;
string -= 2;

while (left < string)
{
ch = *left;
*left++ = *string;
*string-- = ch;
}

return(start);
}

读书人网 >C语言

热点推荐