Problem statement:- Program to Count repeated characters in a string.
Data requirement:-
Input Data:- str
Output Data:- i, arr
Output Data:- i, arr
Additional Data:- i,len
Program in C
Here is the source code of the C Program to Count repeated characters in a string.
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[30];
printf("Enter your String:");
scanf("%[^\n]",str);
int i;
int arr[256]={0};
for(i=0;i<strlen(str);i++)
{
if(str[i]==' ')
continue;
arr[str[i]]++;
}
printf("Repeated character in a string are:\n");
for(i=0;i<256;i++)
{
if(arr[i]>1)
{
printf("%c occurs %d times\n",(char)(i),arr[i]);
}}
}
#include<string.h>
int main()
{
char str[30];
printf("Enter your String:");
scanf("%[^\n]",str);
int i;
int arr[256]={0};
for(i=0;i<strlen(str);i++)
{
if(str[i]==' ')
continue;
arr[str[i]]++;
}
printf("Repeated character in a string are:\n");
for(i=0;i<256;i++)
{
if(arr[i]>1)
{
printf("%c occurs %d times\n",(char)(i),arr[i]);
}}
}
Enter your String:csinfodotcom
Repeated character in a string are:
c occurs 2 times
o occurs 3 times
Repeated character in a string are:
c occurs 2 times
o occurs 3 times
Program in C++
Here is the source code of the C++ Program to Count repeated characters in a string.
Code:
#include<iostream>
using namespace std;
int main()
{
string str;
cout<<"Enter your String:";
getline(cin,str);
int len=0;
while(str[len]!='\0')
{
len++;
}
int arr[256]={0},i;
for(i=0;i<len;i++)
{
if(str[i]==' ')
continue;
arr[str[i]]++;
}
cout<<"Repeated character in a string are:\n";
for(i=0;i<256;i++)
{
if(arr[i]>1)
{
cout<<(char)(i)<<" occurs "<<arr[i]<<" times\n";
}}
}
using namespace std;
int main()
{
string str;
cout<<"Enter your String:";
getline(cin,str);
int len=0;
while(str[len]!='\0')
{
len++;
}
int arr[256]={0},i;
for(i=0;i<len;i++)
{
if(str[i]==' ')
continue;
arr[str[i]]++;
}
cout<<"Repeated character in a string are:\n";
for(i=0;i<256;i++)
{
if(arr[i]>1)
{
cout<<(char)(i)<<" occurs "<<arr[i]<<" times\n";
}}
}
Input/Output:
Enter your String:c plus plus
Repeated character in a string are:
l occurs 2 times
p occurs 2 times
s occurs 2 times
u occurs 2 times
Repeated character in a string are:
l occurs 2 times
p occurs 2 times
s occurs 2 times
u occurs 2 times
Program in Java
Here is the source code of the Java Program to Count repeated characters in a string.
Code:
import java.util.Scanner;
public class p21 {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
System.out.println("Enter your String:");
str1=cs.nextLine();
char[] str=str1.toCharArray();
int i;
int arr[]=new int[256];
for(i=0;i<str.length;i++)
{
if(str[i]==' ')
continue;
arr[str[i]]++;
}
System.out.println("Repeated character in a string are:");
for(i=0;i<256;i++)
{
if(arr[i]>1)
{
System.out.println((char)(i)+" occurs "+arr[i]+" times");
}}
cs.close();
}}
public class p21 {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
System.out.println("Enter your String:");
str1=cs.nextLine();
char[] str=str1.toCharArray();
int i;
int arr[]=new int[256];
for(i=0;i<str.length;i++)
{
if(str[i]==' ')
continue;
arr[str[i]]++;
}
System.out.println("Repeated character in a string are:");
for(i=0;i<256;i++)
{
if(arr[i]>1)
{
System.out.println((char)(i)+" occurs "+arr[i]+" times");
}}
cs.close();
}}
Input/Output:
Enter your String:
java string problem
Repeated character in a string are:
a occurs 2 times
r occurs 2 times
java string problem
Repeated character in a string are:
a occurs 2 times
r occurs 2 times
Program in Python
Here is the source code of the python Program to Count repeated characters in a string.
Code:
str=input("Enter the String:")
arr=[0]*256
for i in range(len(str)):
if str[i]==' ':
continue
num=ord(str[i])
arr[num]+=1
print("Repeated character in a string are:")
for i in range(256):
if arr[i]>1:
print((chr)(i)," occurs ",arr[i]," times")
Input/Output:arr=[0]*256
for i in range(len(str)):
if str[i]==' ':
continue
num=ord(str[i])
arr[num]+=1
print("Repeated character in a string are:")
for i in range(256):
if arr[i]>1:
print((chr)(i)," occurs ",arr[i]," times")
Enter the String:Count repeated characters in a string using python
Repeated character in a string are:
a occurs 4 times
c occurs 2 times
e occurs 4 times
g occurs 2 times
h occurs 2 times
i occurs 3 times
n occurs 5 times
o occurs 2 times
p occurs 2 times
r occurs 4 times
s occurs 3 times
t occurs 5 times
u occurs 2 times
Repeated character in a string are:
a occurs 4 times
c occurs 2 times
e occurs 4 times
g occurs 2 times
h occurs 2 times
i occurs 3 times
n occurs 5 times
o occurs 2 times
p occurs 2 times
r occurs 4 times
s occurs 3 times
t occurs 5 times
u occurs 2 times
0 Comments
Please do not Enter any spam link in the comment box