我正在写一个简单的c程序,它反转一个字符串,从argv [1]中取出字符串.这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* flip_string(char *string){
int i = strlen(string);
int j = 0;
// Doesn't really matter all I wanted was the same size string for temp.
char* temp = string;
puts("This is the original string");
puts(string);
puts("This is the \"temp\" string");
puts(temp);
for(i; i>=0; i--){
temp[j] = string[i]
if (j <= strlen(string)) {
j++;
}
}
return(temp);
}
int main(int argc, char *argv[]){
puts(flip_string(argv[1]));
printf("This is the end of the program\n");
}
Run Code Online (Sandbox Code Playgroud)
基本上就是这样,程序编译并且一切都没有返回到最后的临时字符串(只是空格).在开始时,当它等于字符串时,它会打印temp.此外,如果我在for循环中使用temp的字符printf执行字符,则打印正确的临时字符串即字符串 …