使用strncmp()函數比較兩個字符串指定數量的字符

#include <stdio.h>
#include <string.h>

char str1[] = "The first string.";
char str2[] = "The second string.";

int main(){
  size_t n, x;
  puts(str1);
  puts(str2);
  for(;;){
    puts("\n\nEnter number of characters t compare, 0 to exit.");
    scanf("%d", &n);
    if(n <= 0)
      break;
    x = strncmp(str1, str2, n);
    printf("\nComparing %d characters, strncmp() return %d.", n, x);
  }
  
  return 0;
}
相關文章
相關標籤/搜索