在反转部分字符串时遇到问题
Having trouble reversing parts of a string
当我决定尝试时,这似乎是一个简单的想法,但知道它让我抓狂。
我可以反转整个字符串,但现在我正在尝试反转字符串的各个部分。
示例:
"pizza is amazing" to "azzip si amazing"
基本上我的程序应该将字符串从 点 a 反转到 b,分别处理其中的任何单词。我的逻辑似乎是正确的(至少对我而言),但显然有问题,因为我的输出只是第一个单词 "pizza"
.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *reverse(char *a, int i, int j){ //reverse the words
char temp;
while(i<j){
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
return a;
}
char *words(char *a, int i, int j){ // identify if there are any words from a-b
int count = i;
while(i<j){
if(a[i] == ' '){ // a space signifies the end of a word
reverse(a , i-count, i);
count = 0; //reset count for next word
}
i++;
count++;
}
return a;
}
int main(){
char a[50];
char *a2;
printf("Enter a string:\n); //string input
scanf("%s", a);
int strlength = strlen(a) + 1;
a2 = (char *)malloc(strlength*sizeof(char));
strcpy( a2, a);
printf("Reversed string:\n%s", words(a, 0, 4)); // create a-b range
return 0;
}
我意识到我的问题很可能在 words()
内。我没主意了。
问题一:
你应该更小心地命名变量,易于理解和有意义的名字有助于程序员和其他人阅读你的代码。请记住这一点非常重要。
问题二:
当您将参数%s
传递给scanf()
时,它会读取后续字符,直到找到一个空格(空格字符被认为是空白、换行符和制表符)。
您可以使用 scanf("%[^\n]", a)
读取所有字符,直到找到换行符。
有关 scanf()
的进一步参考,请查看 here。
问题三:
看看 words()
函数,您没有存储基本索引(从哪里开始反转)。对 reverse()
的调用告诉它反转单个字符(没有任何变化)。
您没有指定整个单词是否必须在范围内才能反转,或者即使它在边缘(例如:一半在里面,一半在外面)。我假设整个单词必须在范围内,查看 words()
函数的修改版本:
char *words(char *str, int fromIndex, int toIndex){
int i = fromIndex;
int wordStartingIndex = fromIndex;
/*
It is necessary to expand the final index by one in order
get words bounded by the specified range. (ex: pizza (0, 4)).
*/
toIndex += 1;
/* Loop through the string. */
while(i <= toIndex){
if(str[i] == ' ' || str[i] == '[=10=]' || str[i] == '\n'){
reverse(str, wordStartingIndex, i-1);
wordStartingIndex = (i + 1);
}
i++;
}
return str;
}
这应该可以帮助您入门。功能还不完善,需要修改才能处理一些特殊情况,比如我说的那个。
当我决定尝试时,这似乎是一个简单的想法,但知道它让我抓狂。 我可以反转整个字符串,但现在我正在尝试反转字符串的各个部分。 示例:
"pizza is amazing" to "azzip si amazing"
基本上我的程序应该将字符串从 点 a 反转到 b,分别处理其中的任何单词。我的逻辑似乎是正确的(至少对我而言),但显然有问题,因为我的输出只是第一个单词 "pizza"
.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *reverse(char *a, int i, int j){ //reverse the words
char temp;
while(i<j){
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
return a;
}
char *words(char *a, int i, int j){ // identify if there are any words from a-b
int count = i;
while(i<j){
if(a[i] == ' '){ // a space signifies the end of a word
reverse(a , i-count, i);
count = 0; //reset count for next word
}
i++;
count++;
}
return a;
}
int main(){
char a[50];
char *a2;
printf("Enter a string:\n); //string input
scanf("%s", a);
int strlength = strlen(a) + 1;
a2 = (char *)malloc(strlength*sizeof(char));
strcpy( a2, a);
printf("Reversed string:\n%s", words(a, 0, 4)); // create a-b range
return 0;
}
我意识到我的问题很可能在 words()
内。我没主意了。
问题一:
你应该更小心地命名变量,易于理解和有意义的名字有助于程序员和其他人阅读你的代码。请记住这一点非常重要。
问题二:
当您将参数%s
传递给scanf()
时,它会读取后续字符,直到找到一个空格(空格字符被认为是空白、换行符和制表符)。
您可以使用 scanf("%[^\n]", a)
读取所有字符,直到找到换行符。
有关 scanf()
的进一步参考,请查看 here。
问题三:
看看 words()
函数,您没有存储基本索引(从哪里开始反转)。对 reverse()
的调用告诉它反转单个字符(没有任何变化)。
您没有指定整个单词是否必须在范围内才能反转,或者即使它在边缘(例如:一半在里面,一半在外面)。我假设整个单词必须在范围内,查看 words()
函数的修改版本:
char *words(char *str, int fromIndex, int toIndex){
int i = fromIndex;
int wordStartingIndex = fromIndex;
/*
It is necessary to expand the final index by one in order
get words bounded by the specified range. (ex: pizza (0, 4)).
*/
toIndex += 1;
/* Loop through the string. */
while(i <= toIndex){
if(str[i] == ' ' || str[i] == '[=10=]' || str[i] == '\n'){
reverse(str, wordStartingIndex, i-1);
wordStartingIndex = (i + 1);
}
i++;
}
return str;
}
这应该可以帮助您入门。功能还不完善,需要修改才能处理一些特殊情况,比如我说的那个。