[英]Reverse string function in C?
Here's a C source I tried to test:这是我尝试测试的 C 源代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main()
{
char c[81];
gets(c);
str_inv(c);
puts(c);
}
void str_inv(char *s[])
{
int i, j;
char *temp;
temp=calloc(1 ,strlen(s)*sizeof(char));
if(temp==NULL)
{
fprintf(stderr, "Error: Memory not allocated.");
exit(1);
}
for(i=0, j=strlen(s)-1; i<strlen(s); i++, j--)
{
temp[i]=s[j];
}
for(i=0; i<strlen(s); i++)
{
s[i]=temp[i];
}
free(temp);
}
An output of the program looks like this:程序的输出如下所示:
**abc**
**Process returned 0 (0x0) execution time : 2.262 s**
**Press any key to continue.**
The code in function str_inv
works fine while in main()
function, but not in separate function.函数
str_inv
的代码在main()
函数中工作正常,但在单独的函数中不起作用。
What is the problem with function?功能有什么问题?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.