argv[1] から文字列を取得して、文字列を反転する単純な C プログラムを作成しています。コードは次のとおりです。
#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");
}
基本的にはそれだけです。プログラムはコンパイルされますが、最後に一時文字列は返されません (空白のみ)。最初は、文字列と等しい場合に temp fine を出力します。さらに、for ループで temp の文字ごとに printf を実行すると、印刷された ie 文字列の正しい一時文字列が逆になります。標準出力に出力しようとすると( for ループの後またはメインで)何も起こらず、空白だけが出力されます。
ありがとう