2014-03-06 55 views
2

對於爲例我需要INVERS 「巴黎」 到 「siraP」 ......反向字符字陣列

我的主:

int main(void) 
{ 
    char w1[] = "Paris"; 
    ReverseWord(w1); 
    printf("The new word is: %s",w1); 
    return0; 
} 

和我的功能:

void ReverseWord(char *Str) 
{ 
    int counter=0; 
    for(int i=0; *(Str+i)!='\0'; i++) 
      counter++; 

    int length = counter-1; 

    char temp[length]; 

    for(int j=0; temp[j]=='\0'; j++) 
      temp[j]=Str[length-j]; 
} 

現在我在temp []中有我的逆向詞。 我需要把它放在我的指針* Str中。 我該怎麼做?

感謝

+2

請注意,您不需要使用臨時存儲對於這一點,你可以換字符,而不是。 – unwind

+0

另外,你的循環是不正確的:你在分配它之前檢查'temp [j]'。條件應該是'Ĵ

+1

如果是Windows u可以使用strrev() – Balu

回答

3

如果你想使用temp必須那麼你的函數類似這樣的

void ReverseWord(char *Str) 
{ 
    int i,j; 

    if(str) 
    { 
     int length=strlen(Str); 
     char temp[length+1]; 

     for(j=0; j<length; j++) 
      temp[j]=Str[length-1-j]; 

     temp[j]='\0'; 

     strcpy(Str,temp); 
    } 

} 

不使用temp如下

void ReverseWord(char *Str) 
{ 
    int end= strlen(Str)-1; 
    int start = 0; 

    while(start<end) 
    { 
     Str[start] ^= Str[end]; 
     Str[end] ^= Str[start]; 
     Str[start]^= Str[end]; 

     ++start; 
     --end; 
    } 
} 
+1

'炭溫度[長度初始化+ 1 ]'! –

+0

@IngoLeonhardt非常感謝!編輯回答 –

-1

,你可以這樣做只是通過下面的代碼

for(int k=0;k<strlen(temp);k++) 
    { 
     Str[k]=temp[k]; 
    } 
+0

你剛剛重新創建了一個低效率的strcpy版本。甚至不復制終止0 –

+0

但問題是關於「如何能明確完成」 – YakRangi

+0

沒有,閱讀的問題。而你的方法仍然是低效和錯誤的。 –

0

還有一個選項,這次是危險的malloc(3)。

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

char *rev(char s[]) { 
    char *buf = (char *)malloc(sizeof(char) * strlen(s)); 
    int i, j; 

    if(buf != NULL) 
    for(i = 0, j = strlen(s) - 1; j >= 0; i++, j--) 
     buf[i] = s[j]; 

    return buf; 
} 

int main(int argc, char **argv) { 
    printf("%s\n", rev(argv[1])); 
    return 0; 
} 

運行與"foo bar foobar baz"並獲得zab raboof rab oof回:

~/tmp$ ./a.out "foo bar foobar baz" 
zab raboof rab oof 
+0

爲什麼malloc危險? –

+0

好吧,壞笑話。只有在沒有正確使用的情況下,它本身並不危險。例如在這種情況下,應該使用strnlen()而不是strlen()來確保malloc()不會作爲參數得到任何荒謬的東西。 –

1
void ReverseWord(char *Str) 
{ 
    size_t len; 
    char temp, *end; 

    len = strlen(Str); 
    if (len < 2) 
     return; 

    end = Str + len - 1; 
    while (end > Str) 
    { 
     temp = *end; 
     *end-- = *Str; 
     *Str++ = temp; 
    } 
} 
+0

最高效的解決方案一票決定。 –

0

在這裏,我想你可以學習兩種算法:

  1. C字符串長度計算:在C的結束字符串是'\ 0'
  2. 如何反轉ac字符串in放置

而且如果您需要測試代碼,則應該在堆或分支中分配測試字符串。如果寫入文字字符串,則可能會遇到總線錯誤,因爲文本字符串保存在只讀存儲器的文本區域中。

而下面的演示:

#include <stdio.h> 
#include <stdlib.h> 

void reverse_string(char* str) 
{ 
    size_t len; 
    char tmp, *s; 
    //Get the length of string, in C the last char of one string is \0 
    for(s=str;*s;++s) ; 
    len = s - str; 

    //Here we use the algorithm for reverse the char inplace. 
    //We only need a char tmp place for swap each char 
    s = str + len - 1; 
    while(s>str){ 
     tmp = *s; 
     *s = *str; 
     *str = tmp; 
     s--; 
     str++; 
    } 
} 

int main() 
{ 
    char* a = "abcd"; 
    //Here "abcd" will be saved in READ Only Memory. If you test code, you will get a bus error. 
    char* b = (char*)calloc(1,10); 
    strcpy(b,a); 
    reverse_string(b); 
    printf("%s\n",b); 

    a = "abcde"; 
    strcpy(b,a); 
    reverse_string(b); 
    printf("%s\n",b); 
}