C Programming: Count of each character in a given string
C String: Exercise-33 with Solution
Write a C programming to count of each character in a given string.
Sample Solution:
C Code:
#include<stdio.h>
#include<string.h>
int if_char_exists(char c, char p[], int x, int y[])
{
int i;
for (i=0; i<=x;i++)
{
if (p[i]==c)
{
y[i]++;
return (1);
}
}
if(i>x) return (0);
}
int main()
{
char str1[80],chr[80];
int n,i,x,ctr[80];
printf("Enter a str1ing: ");
scanf("%s",str1);
n=strlen(str1);
chr[0]=str1[0];
ctr[0]=1;
x=0;
for(i=1;i < n; i++)
{
if(!if_char_exists(str1[i], chr, x, ctr))
{
x++;
chr[x]=str1[i];
ctr[x]=1;
}
}
printf("The count of each character in the string %s is \n", str1);
for (i=0;i<=x;i++)
printf("%c\t%d\n",chr[i],ctr[i]);
}
Sample Output:
Enter a str1ing: The count of each character in the string w3resource is w 1 3 1 r 2 e 2 s 1 o 1 u 1 c 1
Flowchart :
C Programming Code Editor:
Improve this sample solution and post your code through Disqus.
Previous: Write a C programming to find the repeated character in a given string.
Next: Write a C programming to convert vowels into upper case character in a given string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
C Programming: Tips of the Day
Static variable inside of a function in C
The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().
The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.
The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().
Ref : https://bit.ly/3fOq7XP
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework