2016-09-30 3 views
1

argv[1]の値といくつかのテストケースを比較する方法を理解しようとしています。 argv[1]が特定のchar値で終了するかどうかを確認したいと思います。argv [1]が特定の文字で終わるかどうかを調べる方法は?

int main(int argc, char * argv[]) 
{ 
char strin[250]; 
int length; 
printf("The argument supplied is %s\n", argv[1]); 
strcpy(strin,argv[1]); 
length = strlen(strin); 
printf("Testing: %c",strin[length]); 
    if(strin[length] = 'b') 
    { 
    printf("b in the input"); 
    } 

} 

しかし、私はprint文は、トリガー任意の入力に置くたびに、いくつかの理由のために:これまでのところ私は、次のコードを持っています。コマンドライン引数の最後の文字が、それと等しく設定した文字と等しいかどうかを確認するにはどうすればよいですか?

+1

'strin [長さ]' - > ' strin [length-1] ' –

+0

はテストで=の代わりに==を使用します。 –

+1

割り当て:' strin [length] = 'b'';比較: 'strin [length] == 'b'' – pmg

答えて

1

基本的に、あなたが必要とするすべてが行うことですが、以下である:今

int main(int argc, char * argv[]) { 
    // check if there's an argument to test 
    if (1 > argc) { 
     // extract the position of the last character 
     int last_pos = strlen(argv[1])-1; 
     // compare the last character with the character "b" 
     if (0 <= last_pos && 'b' == argv[1][last_pos]) { 
      printf("Hoora! The input ends with b!"); 
      return 0; 
     } else { 
      printf("Bummer… The input does not end with b :("); 
     } 
    } else { 
     printf("there's no argument to test!"); 
    } 
} 

、ここで間違っている内容の要約です:

int main(int argc, char * argv[]) 
{ 
char strin[250]; 
int length; 
printf("The argument supplied is %s\n", argv[1]); 

// you're doing a copy from the first argument into the 
// variable strin. If argv[1] is 251 characters, you'll 
// overwrite memory, and will cause a "buffer overflow". 
// Whenever you need to do strcpy of data input by a user 
// use strncpy(). 
strcpy(strin,argv[1]); 

// you're extracting the /length/ of the string, not the /position/ 
// of the last character, so when you're trying to access at index 
// length, you'll get data from one character beyond the array. 
length = strlen(strin); 

// so here you're seeing a random value from the memory of your computer 
printf("Testing: %c",strin[length]); 

// here you made a mistake and you're assigning the value 'b' to the 
// value beyond the allocated memory for the array. Basically: 
// Here be dragons. 

// To avoid that mistake, always put the value you're comparing against 
// in a comparaison on the Left Hand Side, and the value you're comparing 
// on the right hand side. Then the compiler will yell at you! 
    if(strin[length] = 'b') 
    { 
    printf("b in the input"); 
    } 

} 
+1

'argv [1]'が長さ0でないことを確認するのを忘れました( 'last_pos'は負であるかもしれません)。 – rabensky

+0

ありがとう、固定! – zmo

+0

'0 <= last_pos'; P – rabensky

0

文字列は0で終了し、インデックスは0から始まります。ただし、strlen()はターミネータをカウントしません。

したがって、strin[length]は常に0ターミネータです。最後の文字を取得するには、strin[length - 1]が必要です。もちろん、length > 0が真の場合にのみ実行できます。

Cの比較は==演算子を使用して行われます。単一の=は、あなたが望むものではありません。

また、文字列をコピーする際に意味がないので、argv[1]を直接チェックすることができます。 strlen() return size_tではなく、intです。

1

C配列はゼロインデックスベースです。最初の要素にアクセスするには、array[0]を、10番目の要素にアクセスするにはarray[9]を実行します。

printf("Testing: %c",strin[length]); 

これは、NULLターミネータ\0であることを起こる文字列の最後の文字の後の文字1を出力します。これは比較しません

if(strin[length] = 'b') 
{ 
    printf("b in the input"); 
} 

、あなたの代わりに==を使用する必要があります(つまり、Cの文字列がどのように動作するかです)。これはまた、上記と同じ問題を抱えている。

したがって、アクセスを[length - 1]に変更し、==を使用してください。

+0

配列がゼロインデックス化されている:うん、しかし、なぜあなたはユーザー入力のために 'argv [0]'をチェックしたいのですか?その値は、起動プロセス(つまりシェル)によって決定されます。 _user_で指定された引数を調べるには、 'argv [1]'で始める必要があります。 –

+0

@EliasVanOotegemそれは私の指摘ではありませんでしたが、OPはlengthがlength要素、それはそうではありません、それは-1です。 –

0

文字列の最後の文字はstrlen() - 1です。strcpyを使用しているため、別の問題があります。引数の文字列が250文字を超える場合、それは非常に危険です。したがって、安全のためにstrncpyを使用する必要があります。

int main(int argc, char * argv[]) 
{ 
char strin[250]; 
int length; 
printf("The argument supplied is %s\n", argv[1]); 
strncpy(strin,argv[1], 250); 
length = strlen(strin); 
printf("Testing: %c",strin[length-1]); 
    if(strin[length] = 'b') 
    { 
    printf("b in the input\n"); 
    } 
} 
+0

最後の文字をテストしたいだけなら、コピーを作成する必要はありません。 – Barmar

+0

@Barmarはい、私は彼のコードが危険であることを彼に示したかったのです。 –

関連する問題