[英]C function, reverse string
I want help regarding a function which you call from main that reverse text. 我需要有关您从主要反向文本调用的功能的帮助。 However, the program works, well "more or less" but it crashes. 但是,该程序可以“或多或少”运行,但是会崩溃。 Here is how code looks 这是代码的样子
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void reverse(char * array, int numberOfChars) {
int begin = 0;
int end = 0;
char temp;
end = strlen(&array) - 1;
printf("%s", &array);
while (begin < end) {
temp = array[begin];
array[begin] = array[end];
array[end] = temp;
begin++;
end--;
}
}
int main() {
reverse('supm', 4);
return(0);
getchar();
}
The string gets reversed to mpus, but then crashes, apparently it seems also like the arrays can only take in 4 characters, if i change it to 5 and the integer value to 5, it won't work at all. 字符串被反转为mpus,但随后崩溃,显然好像数组也只能接受4个字符,如果我将其更改为5并将整数值更改为5,则根本无法使用。 Any help would be appreciated. 任何帮助,将不胜感激。
strlen(&array)
and printf("%s", &array);
strlen(&array)
和printf("%s", &array);
are wrong. 错了。 They will pass where the pointer is instead of where the string is. 它们将通过指针而不是字符串的位置。 use strlen(array)
and printf("%s", array);
使用strlen(array)
和printf("%s", array);
. 。
reverse('supm', 4);
is also wrong because the first argument of this call is not a string but an implementation-defined integer. 这也是错误的,因为此调用的第一个参数不是字符串,而是实现定义的整数。
Be careful that just changing it to reverse("supm", 4);
请注意,只需将其更改为reverse("supm", 4);
won't work because modifying string literals isn't allowed. 无法使用,因为不允许修改字符串文字。
Also, the last getchar();
另外,最后一个getchar();
won't be executed because it is after return 0;
将不会执行,因为它在return 0;
. 。
Try this: 尝试这个:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void reverse(char * array, int numberOfChars) {
int begin = 0;
int end = 0;
char temp;
end = strlen(array) - 1;
printf("%s", array);
while (begin < end) {
temp = array[begin];
array[begin] = array[end];
array[end] = temp;
begin++;
end--;
}
}
int main() {
char supm[] = "supm";
reverse(supm, 4);
puts(supm); /* added this to see if this code is working well */
return(0);
}
Maybe you should use the parameter numberOfChars
instead of strlen(array)
because you pass the parameter, but it is not used at all. 也许您应该使用参数numberOfChars
而不是strlen(array)
因为您传递了参数,但根本没有使用它。
A few things immediately pop out at me: 我突然想到了几件事:
char*
, where you are passing a char**
. strlen应该带一个char*
,而您要传递一个char**
。 Instead of strlen(&array)
, use strlen(array)
. 代替strlen(&array)
,使用strlen(array)
。 printf
. 与printf
类似的故事。 Instead of printf("%s", &array)
, use printf("%s", array)
. 代替printf("%s", &array)
,使用printf("%s", array)
。 'supm'
should be "supm"
. 您的'supm'
应该是"supm"
。 You also cannot modify a string literal, so you'll need to set a variable containing your string and then pass the variable instead. 您也不能修改字符串文字,因此您需要设置一个包含字符串的变量,然后传递该变量。 return(0)
before calling getchar
. 您在调用getchar
之前return(0)
。 That means getchar
will never be called. 这意味着将永远不会调用getchar
。 numberOfChars
is unused. numberOfChars
未使用。 Either remove that parameter or use it instead of strlen
. 删除该参数或使用它代替strlen
。 In your function reverse
- 在您的功能reverse
-
end = strlen(&array) - 1;
printf("%s", &array);
You pass char **
to strlen
which expects a const char *
and try to print a char **
with %s
. 您将char **
传递给strlen
,它需要一个const char *
然后尝试用%s
打印一个char **
。 Jusr pass array
to both these functions. Jusr将array
传递给这两个函数。 And in main
- 在main
-
reverse('supm', 4);
If you even do "spum"
which would be string literal , thus constant (modifying it would cause problem). 如果您甚至执行"spum"
,这将是字符串literal,因此为常量(修改它会导致问题)。
Do this instead - 改为这样做-
char s[]="spum";
reverse(s, 4);
Compiler must have issued warnings for these statements and if not then enable compiler warnings. 编译器必须为这些语句发出警告,如果没有,则启用编译器警告。
array
is already a pointer-- &array
gives you a pointer to the pointer. array
已经是一个指针了&array
为您提供了一个指向该指针的指针。 so change the strlen call to this: 因此,将strlen调用更改为:
strlen(array);
you need to define the string before you pass to the function-- otherwise the string only exists inside your function, you won't be able to access it outside. 您需要在传递给函数之前定义字符串-否则,该字符串仅存在于函数内部,您将无法在外部访问它。
char mystr[] = "supm"; reverse(mystr, 4); printf("%s\\n", mystr); return 0;
Try this code I have fixed all your code problems. 试试这个代码,我已经解决了您所有的代码问题。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void reverse(char * array, int numberOfChars) {
int begin = 0;
int end = 0;
char temp;
end = strlen(array) - 1;
printf("%s\n", array);
while (begin < end) {
temp = array[begin];
array[begin] = array[end];
array[end] = temp;
begin++;
end--;
}
}
int main() {
char str[] ="sump";
reverse(str, 4);
puts(str);
return(0);
}
What you can learn from problems of this code ? 您可以从这段代码的问题中学到什么?
void reverse(char * array, int numberOfChars) /* definition */ void reverse(char *数组,int numberOfChars)/ *定义* /
reverse('supm', 4); reverse('supm',4); /*calling function */ / *调用函数* /
Compare this both two in definition first argument is to pass character array char *array
But in calling you are passing 'sump'
.Now onwards please remember for more than character you can use ""
so here you should use "sump"
比较这两个定义,第一个参数是传递字符数组char *array
但在调用时传递'sump'
是'sump'
。现在,请记住除了字符以外,您还可以使用""
因此这里应使用"sump"
end = strlen(&array) - 1; 结束= strlen(&array)-1;
Its argument is constant character pointer . 它的参数是常量字符指针。 But here you are passing &array
.I guess you think &array
is address of array but that is wrong for array array
represent address and array[i]
represent value 但是在这里,您传递了&array
。我想您认为&array
是&array
的地址,但是对于数组array
代表地址和array[i]
代表值是错误的
getchar() getchar()
This function is for reading input not for printing strings. 此功能用于读取输入,而不用于打印字符串。 refer getchar 参考getchar
For printing reversed output you should use puts at last not getchar
对于打印反向输出,您应该最后使用puts而不是getchar
In your main
function, the line reverse('supm', 4);
在您的main
功能中,行reverse('supm', 4);
has a couple of things wrong with it. 有一些问题。 First of all, it is a character constant, not a string, so you will end up passing an invalid pointer value to your reverse
function. 首先,它是一个字符常量,而不是字符串,因此您最终会将无效的指针值传递给reverse
函数。 You probably meant to use "supm"
which is a string and does match the char *
expected by your reverse
function. 您可能打算使用"supm"
,它是一个字符串,并且与reverse
函数期望的char *
匹配。 However, "supm"
is a string literal constant, so although you can point to it, you are not allowed to modify it. 但是, "supm"
是字符串文字常量,因此尽管可以指向它,但不允许对其进行修改。 So you should do something like this: 因此,您应该执行以下操作:
char rev[] = "supm";
reverse(rev, 4);
Here, rev
is an array of 5 char
, initialized as {'s', 'u', 'p', 'm', '\\0'}
. 在这里, rev
是由5个char
的数组,初始化为{'s', 'u', 'p', 'm', '\\0'}
。
You are not currently using the numberOfChars
parameter of reverse
, so you could remove it. 您当前未使用reverse
的numberOfChars
参数,因此可以将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.