Developer community
0 votes
21.1k views
c program to find repeated characters in a string.
asked by Sr Member (3.6k points) 1 2 8 | 21.1k views

1 Answer

0 votes
Best answer
#include<stdio.h>
 
int main(){
    char str[40];
    int i, j = 0;
    scanf("%s", str);
    for(i = 1; str[i] != '\0';i++){
        if(str[i-1] == str[i]){//For consecutive repeated characters
            j = i-1;        //If found, we place an index j there
            j++;
            while(1){           //This loops checks for mutiple
                if(str[j] == str[j+1])  //occurrences of a character
                    j++;
                else
                    break;
            }
            printf("Character %c is repeated %d times\n", str[i], j - (i-1)+1);
            i = j;
        }
    }
    if(j == 0){
        printf("No repeated character");
    }
    return 0;
}
 

If you wanna find out all the repeated characters, first sort the string lexicographically.

Then use the logic used in the above program to find repeated characters and their number of occurrences

 

answered by Sr Member (3.6k points) 1 2 8
How can we do that with a two dimensional array [12][40]?

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

3,751 questions
4,046 answers
117 comments
10,709 users