コマンドラインから入力された文字列に手を加えて新しい文字列にして出力するプログラムを作りたいです
読み込んだ文字列の末尾に.dxをつけ、もし読み込んだ文字列の末尾が.exだった場合は末尾の.exを.dxに置き換えて出力します
c
1#include<stdio.h> 2#include<stdlib.h> 3#include<string.h> 4int main(int argc, char *argv[]) { 5 int i; 6 char *string; 7 8 for (i = 1; i < argc; i++) { 9 string = malloc(100 * sizeof(char)); 10 string = argv[i]; 11 printf("%s", string); 12 if (strcmp(string+strlen(string) - 3, ".ex") == 0) /*後ろから3文字目以降が.exになっているかをチェック*/{ 13 strcpy(string + strlen(string) - 3, ".dx");/*後ろから3文字目以降を.dxに置き換える*/ 14 } 15 else strcat(string,".dx");/*末尾3文字が.ex出ない場合はそのまま.dxを付け足す*/ 16 printf("-> %s\n", string); 17 free(string); 18 } 19 return 0; 20}
コマンドライン引数を a abc abc.ex abc.ex.ex abc.dx とすると得たい結果は
a -> a.dx
abc-> abc.dx
abc.ex.ex->abc.ex.dx
abc.dx->abc.dx.dx
なのですが、a->a.dxの時点でエラーになって強制終了してしまいます。
詳しい方指摘の方よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/15 05:43