2016-09-30 231 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 [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()返回size_t,而不是int

1

C數組是基於零索引的。要訪問你的第一個元素array[0],要訪問第10個你做的array[9]

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

這會打印出字符串,這恰好是空終止\0在最後一個字符之後的字符1。 (這是C字符串是如何工作的)

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

這不比較,你應該使用==代替。這也會遇到與上述相同的問題。

因此,將您的訪問權限更改爲[length - 1]並使用==

+0

在被零索引的數組上:是的,但是你爲什麼要檢查用戶輸入的'argv [0]'?其值由啓動過程(即外殼)決定。要檢查_user_提供的參數,你必須從'argv [1]' –

+0

@EliasVanOotegem開始這不是我的觀點,我的觀點是OP可能認爲[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是的,但我想告訴他他的代碼很危險。 –