Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- are_all_string_characters_different.c
- Are all characters in string different from each other.
- Check if all characters in string are distinct (unique).
- Check if string contains all unique or distinct characters.
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <string.h>
- // returns 1 if all characters in string are distinct (unique, different from each other)
- // otherwise returns 0
- int are_all_string_characters_different( char str[] )
- {
- int i, j, len = strlen(str);
- for (i=0; i<len; i++)
- for (j=i+1; j<len; j++)
- if (str[i] == str[j])
- return 0;
- return 1;
- }
- int main(void)
- {
- char str1[] = "String 1";
- char str2[] = "Second string";
- printf("\n str1 = \"%s\" \n", str1);
- if ( are_all_string_characters_different(str1) )
- printf("\n All string characters are distinct (unique, different from each other). \n");
- else
- printf("\n There are repeating characters in the string. \n");
- printf("\n str2 = \"%s\" \n", str2);
- if ( are_all_string_characters_different(str2) )
- printf("\n All string characters are distinct (unique, different from each other). \n");
- else
- printf("\n There are repeating characters in the string. \n");
- return 0;
- } // main()
Add Comment
Please, Sign In to add comment