[英]Bug in my C code to reverse words in a string
我正在練習一些編程問題,並試圖編寫流行的“字符串中的反向詞”問題。
我試圖在C中提出我自己的代碼。我能夠部分地做到正確。 也就是說,“hello world”成為“世界olleh”。 我想知道這里的bug是什么。 我覺得某個地方我正在創造一個錯誤的錯誤。
盡可能地,我想在不使用庫函數的情況下完成它。 我在這里搜索了這個問題並找到了很多解決方案,但我想知道為什么我的解決方案不起作用。
這是代碼:
#include <stdio.h>
#include <string.h>
void reverse(char*, int);
int main(int argc, char **argv)
{
char st[]= "hello world";
int len = strlen(st);
int i=0,j=0;
reverse(st,len-1); // Reverse the entire string. hello world => dlrow olleh
while(st[j]){ //Loop till end of the string
if ( *(st+j) == ' ' || *(st+j) == '\0' ) { //if you hit a blank space or the end of the string
reverse(st+i,j-1); // reverse the string starting at position i till position before the blank space i.e j-1
i=++j; //new i & j are 1 position to the right of old j
}
else {
j++; //if a chacacter is found, move to next position
}
}
printf("%s",st);
return 0;
}
void reverse(char *s, int n)
{
char *end = s+n; //end is a pointer to an address which is n addresses from the starting address
char tmp;
while (end>s) //perform swap
{
tmp = *end;
*end = *s;
*s = tmp;
end--;
s++;
}
}
謝謝!
更新 :根據@Daniel Fischer的回答,這是正確的實現: http ://ideone.com/TYw1k
問題是
while(st[j]){ //Loop till end of the string
if ( *(st+j) == ' ' || *(st+j) == '\0' )
while
條件阻止在字符串末尾輸入循環,因此最后一個單詞不會再次反轉。
您可以將其設為無限循環,然后添加
if (st[j] == '\0') break;
在反轉之后,或者在while循環離開后反轉最后一個單詞。
你確實有一個錯誤:電話
reverse(st+i,j-1);
應該
reverse(st+i,j-i-1);
你的代碼傳遞j-1
,它是從字符串開頭到最后一個空格位置的長度; 它應該是最后一個單詞的長度,所以你需要減去第一個字符的索引(即i
)。
你也沒有倒轉最后一個字(詳見其他答案 )。
我認為你想要反轉字符串中的單詞,而不是反轉整個字符串然后反轉單個單詞。 因此,刪除第一個反向,然后應用上面的建議更改。
#include <stdio.h>
#include <string.h>
void reverse(char*, int);
int main(int argc, char **argv)
{
char st[]= "hello world";
int i=0, j=0;
while(st[j]){ //Loop till end of the string
if ( st[j] == ' ') { //if you hit a blank space or the end of the string
reverse(&st[i], j - i - 1); // reverse the string starting at position i till position before the blank space i.e j-1
i = ++j; //new i & j are 1 position to the right of old j
}
else {
j++; //if a chacacter is found, move to next position
}
}
reverse(&st[i], j - i - 1);
printf("%s\n",st);
return 0;
}
void reverse(char *s, int n)
{
char *end = s + n; //end is a pointer to an address which is n addresses from the starting address
char tmp;
while (end > s) //perform swap
{
tmp = *end;
*end = *s;
*s = tmp;
end--;
s++;
}
}
輸入字符串為'\\ 0'或類似'Hello world'時要小心。 上面的代碼沒有管理這種情況。 想一想!
@RBK:你首先拿一個字符串,反轉它,然后根據特定的單詞再次反轉它們。 我采用了一種略微不同的做法。 如果需要,我取字符串然后反轉,否則我復制相同的單詞。
int main(int argc, char*argv[])
{
char *p,st[]= "hello world";
char buf[12]={0};
char fstr[12]={0};
int i=0,j=0,k=0,l=0;
for(p=st;*p!='\0';p++){
//Get the Word
buf[i++] = *p;
//Parse the Word
if(*p == ' ' || *(p+1) == '\0'){
buf[i]='\0';
j=i-1;
i=0; //reset counter
if(k){ //reverse word and copy
while(j>=i){
fstr[l++]=buf[j--];
}
k=0;
}
else{ //copy same word
while(i<=j){
fstr[l++]=buf[i++];
}
i=0; //reset counter
k=1;
}
}
}
fstr[l]='\0';
printf("%s\n",fstr);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.