fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define SIZE 100
  5.  
  6. int main(void)
  7. {
  8. char string1[SIZE];
  9. char string2[SIZE];
  10. int characters;
  11.  
  12. printf("Enter the first string: ");
  13. fgets(string1, SIZE, stdin);
  14.  
  15. printf("Enter the second string: ");
  16. fgets(string2, SIZE, stdin);
  17.  
  18. string1[strcspn(string1, "\n")] = '\0';
  19. string2[strcspn(string2, "\n")] = '\0';
  20.  
  21. printf("Enter the number of characters to compare: ");
  22. scanf("%d", &characters);
  23.  
  24. int result = strncmp(string1, string2, characters);
  25.  
  26. if (result < 0)
  27. {
  28. printf("The compared part of the first string is less than the second.\n");
  29. }
  30. else if (result > 0)
  31. {
  32. printf("The compared part of the first string is greater than the second.\n");
  33. }
  34. else
  35. {
  36. printf("The compared parts of the two strings are equal.\n");
  37. }
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0.01s 5356KB
stdin
Standard input is empty
stdout
Enter the first string: Enter the second string: Enter the number of characters to compare: The compared parts of the two strings are equal.