+ 1

How do you print duplicate characters from a string?

22nd Jun 2020, 4:59 PM
Kumari Shivangi
3 Antworten
+ 1
Algorithm Define a string. Two loops will be used to find the duplicate characters. Outer loop will be used to select a character and initialize variable count by 1. Inner loop will compare the selected character with rest of the characters present in the string. If a match found, it increases the count by 1 and set the duplicates of selected character by '0' to mark them as visited. After inner loop, if count of character is greater than 1, then it has duplicates in the string.
22nd Jun 2020, 5:01 PM
Kumari Shivangi
+ 1
#include <stdio.h>   #include <string.h>       int main()   {       char string[] = "Great responsibility";       int count;              printf("Duplicate characters in a given string: \n");       //Counts each character present in the string       for(int i = 0; i < strlen(string); i++) {           count = 1;           for(int j = i+1; j < strlen(string); j++) {               if(string[i] == string[j] && string[i] != ' ') {                   count++;                   //Set string[j] to 0 to avoid printing visited character                   string[j] = '0';               }           }           //A character is considered as duplicate if count is greater than 1           if(count > 1 && string[i] != '0')               printf("%c\n", string[i]);       }           return 0;   }  
22nd Jun 2020, 5:02 PM
Kumari Shivangi
+ 1
When you have question and the answer as well then why've you posted it in Q&A : https://www.sololearn.com/Discuss/1316935/?ref=app
23rd Jun 2020, 4:35 AM
Daljeet Singh
Daljeet Singh - avatar