dmilicev

are_all_string_characters_different.c

Sep 28th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.30 KB | None | 0 0
  1. /*
  2.  
  3.     are_all_string_characters_different.c
  4.  
  5.     Are all characters in string different from each other.
  6.     Check if all characters in string are distinct (unique).
  7.     Check if string contains all unique or distinct characters.
  8.  
  9.  
  10.     You can find all my C programs at Dragan Milicev's pastebin:
  11.  
  12.     https://pastebin.com/u/dmilicev
  13.  
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18.  
  19. // returns 1 if all characters in string are distinct (unique, different from each other)
  20. // otherwise returns 0
  21. int are_all_string_characters_different( char str[] )
  22. {
  23.     int i, j, len = strlen(str);
  24.  
  25.     for (i=0; i<len; i++)
  26.         for (j=i+1; j<len; j++)
  27.             if (str[i] == str[j])
  28.                 return 0;
  29.  
  30.     return 1;
  31. }
  32.  
  33.  
  34. int main(void)
  35. {
  36.     char str1[] = "String 1";
  37.     char str2[] = "Second string";
  38.  
  39.     printf("\n str1 = \"%s\" \n", str1);
  40.  
  41.     if ( are_all_string_characters_different(str1) )
  42.         printf("\n All string characters are distinct (unique, different from each other). \n");
  43.     else
  44.         printf("\n There are repeating characters in the string. \n");
  45.  
  46.     printf("\n str2 = \"%s\" \n", str2);
  47.  
  48.     if ( are_all_string_characters_different(str2) )
  49.         printf("\n All string characters are distinct (unique, different from each other). \n");
  50.     else
  51.         printf("\n There are repeating characters in the string. \n");
  52.  
  53.     return 0;
  54.  
  55. } // main()
  56.  
Add Comment
Please, Sign In to add comment