在 C 中反转字符串 for 循环错误
Reversing String in C for loop error
我有一个字符串数组,我正在尝试反转数组中的每个字符串以查看该字符串是否为回文。我正在使用 for
循环来递增 int i
(索引)。然而,在我调用反向函数后,i
的值变成了一个非常大的数字,我无法弄清楚为什么会这样。
#include <stdio.h>
#include <string.h>
void revString(char *dest, const char *source);
int main() {
const char *strs[] = {
"racecar",
"radar",
"hello",
"world"
};
int i;
char res[] = "";
for (i = 0; i < strlen(*strs); i++) {
printf("i is %d\n", i);
revString(&res[0], strs[i]); //reversing string
printf("i is now %d\n", i);
//comparing string and reversed string
if (strcmp(res, strs[i]) == 0) {
printf("Is a palindrome");
} else {
printf("Not a palindrome");
}
}
return 0;
}
void revString(char *dest, const char *source) {
printf("%s\n", source);
int len = strlen(source);
printf("%d\n", len);
const char *p;
char s;
for (p = (source + (len - 1)); p >= source; p--) {
s = *p;
*(dest) = s;
dest += 1;
}
*dest = '[=10=]';
}
这是显示调用 revString
函数前后 i
值的输出。
i is 0
i is now 1667588961
Illegal instruction: 4
这是经过一些改动的最终代码
#include <stdio.h>
#include <string.h>
void revString(char* dest, const char* source);
int main(){
const char* strs[] = {
"racecar",
"radar",
"hello",
"world"
};
static int i;
char res[] = "";
int length = (int) sizeof(strs)/sizeof(char*);
for(i = 0; i < length; i++)
{
printf("i is %d\n", i);
revString(&res[0], strs[i]); //reversing string
printf("i is now %d\n", i);
//comparing string and reversed string
if(strcmp(res, strs[i]) == 0){
printf("Is a palindrome");
}else{
printf("Not a palindrome");
}
}
return 0;
}
void revString(char* dest, const char* source){
printf("%s\n", source);
int len = (int) strlen(source);
printf("%d\n", len);
const char* p;
char s;
for(p = (source + (len - 1)); p >= source; p--){
s = *p;
*(dest) = s;
dest += 1;
}
*dest = '[=10=]';
}
更改 1 :-
int i; to static int i; (Reason:- i is local variable you are calling
function so when function call the value of i will remove and after
that it will assign garbage value.)
更改 2 :-
strlen(*strs) to length of array (because strlen(*strs) will give the
length of first string)
您的代码中存在多个问题:
您传递的目标数组 char res[] = "";
对于要反转的字符串来说太小了。它的大小是 1
。这会导致缓冲区溢出,从而导致未定义的行为。
改用char res[20];
。
您枚举的字符串数组的上限不正确。改用这个:
for (i = 0; i < sizeof(strs) / sizeof(*strs); i++)
revString()
中循环的终止测试也不正确:当等于 source
时递减 p
具有未定义的行为,尽管不太可能有后果。你可以这样简化这个函数:
void revString(char *dest, const char *source) {
size_t len = strlen(source);
for (size_t i = 0; i < len; i++) {
dest[i] = source[len - i - 1];
}
dest[len] = '[=11=]';
}
这是生成的代码:
#include <stdio.h>
#include <string.h>
void revString(char *dest, const char *source) {
size_t len = strlen(source);
for (size_t i = 0; i < len; i++) {
dest[i] = source[len - i - 1];
}
dest[len] = '[=12=]';
}
int main(void) {
const char *strs[] = { "racecar", "radar", "hello", "world" };
char res[20];
for (size_t i = 0; i < sizeof(strs) / sizeof(*strs); i++) {
revString(res, strs[i]);
//comparing string and reversed string
if (strcmp(res, strs[i]) == 0) {
printf("Is a palindrome\n");
} else {
printf("Not a palindrome\n");
}
}
return 0;
}
我有一个字符串数组,我正在尝试反转数组中的每个字符串以查看该字符串是否为回文。我正在使用 for
循环来递增 int i
(索引)。然而,在我调用反向函数后,i
的值变成了一个非常大的数字,我无法弄清楚为什么会这样。
#include <stdio.h>
#include <string.h>
void revString(char *dest, const char *source);
int main() {
const char *strs[] = {
"racecar",
"radar",
"hello",
"world"
};
int i;
char res[] = "";
for (i = 0; i < strlen(*strs); i++) {
printf("i is %d\n", i);
revString(&res[0], strs[i]); //reversing string
printf("i is now %d\n", i);
//comparing string and reversed string
if (strcmp(res, strs[i]) == 0) {
printf("Is a palindrome");
} else {
printf("Not a palindrome");
}
}
return 0;
}
void revString(char *dest, const char *source) {
printf("%s\n", source);
int len = strlen(source);
printf("%d\n", len);
const char *p;
char s;
for (p = (source + (len - 1)); p >= source; p--) {
s = *p;
*(dest) = s;
dest += 1;
}
*dest = '[=10=]';
}
这是显示调用 revString
函数前后 i
值的输出。
i is 0
i is now 1667588961
Illegal instruction: 4
这是经过一些改动的最终代码
#include <stdio.h>
#include <string.h>
void revString(char* dest, const char* source);
int main(){
const char* strs[] = {
"racecar",
"radar",
"hello",
"world"
};
static int i;
char res[] = "";
int length = (int) sizeof(strs)/sizeof(char*);
for(i = 0; i < length; i++)
{
printf("i is %d\n", i);
revString(&res[0], strs[i]); //reversing string
printf("i is now %d\n", i);
//comparing string and reversed string
if(strcmp(res, strs[i]) == 0){
printf("Is a palindrome");
}else{
printf("Not a palindrome");
}
}
return 0;
}
void revString(char* dest, const char* source){
printf("%s\n", source);
int len = (int) strlen(source);
printf("%d\n", len);
const char* p;
char s;
for(p = (source + (len - 1)); p >= source; p--){
s = *p;
*(dest) = s;
dest += 1;
}
*dest = '[=10=]';
}
更改 1 :-
int i; to static int i; (Reason:- i is local variable you are calling function so when function call the value of i will remove and after that it will assign garbage value.)
更改 2 :-
strlen(*strs) to length of array (because strlen(*strs) will give the length of first string)
您的代码中存在多个问题:
您传递的目标数组
char res[] = "";
对于要反转的字符串来说太小了。它的大小是1
。这会导致缓冲区溢出,从而导致未定义的行为。改用
char res[20];
。您枚举的字符串数组的上限不正确。改用这个:
for (i = 0; i < sizeof(strs) / sizeof(*strs); i++)
revString()
中循环的终止测试也不正确:当等于source
时递减p
具有未定义的行为,尽管不太可能有后果。你可以这样简化这个函数:void revString(char *dest, const char *source) { size_t len = strlen(source); for (size_t i = 0; i < len; i++) { dest[i] = source[len - i - 1]; } dest[len] = '[=11=]'; }
这是生成的代码:
#include <stdio.h>
#include <string.h>
void revString(char *dest, const char *source) {
size_t len = strlen(source);
for (size_t i = 0; i < len; i++) {
dest[i] = source[len - i - 1];
}
dest[len] = '[=12=]';
}
int main(void) {
const char *strs[] = { "racecar", "radar", "hello", "world" };
char res[20];
for (size_t i = 0; i < sizeof(strs) / sizeof(*strs); i++) {
revString(res, strs[i]);
//comparing string and reversed string
if (strcmp(res, strs[i]) == 0) {
printf("Is a palindrome\n");
} else {
printf("Not a palindrome\n");
}
}
return 0;
}