GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — C++ program to find the frequency of characters in a string
C++ program to find the frequency of characters in a stringIn this article, you will learn and get code to find and print the frequency of characters (single and all) in a string using the C++ language. Here is the list of programs available in this article:
The user must enter both characters and strings at run-time. Note: The frequency of a character in a string is the total number of times the character occurs in the given string. or the occurrence of a character in a string. Find the Frequency of a Character in a StringTo find the frequency of a character in a string using C++ programming, you have to ask the user to enter the string first, and then ask them to enter the character to find its frequency, as shown in the program given below: The question is: write a program in C++ to find the frequency of a character in a string. Here is its answer: #include<iostream> #include<stdio.h> using namespace std; int main() { char str[100], ch; int i=0, freq=0; cout<<"Enter the String: "; gets(str); cout<<"Enter a Character to Find its Frequency: "; cin>>ch; while(str[i]) { if(ch==str[i]) freq++; i++; } cout<<"\nFrequency = "<<freq; cout<<endl; return 0; } This program was built and runs under the Code::Blocks IDE. Here is its sample run: Now supply any string, say codes cracker dot com, and then a character, say c, to find its frequency after pressing the ENTER key. Here is the sample output: The dry run of the above program with the same user input (as provided in the sample run given above) goes like this:
Determine the Frequency of All Characters in a StringThis program asks the user to enter the string alone. Because this program finds and prints the frequency of all characters in a given string. #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int main() { char str[100], ch; int i, j, k, len, freq=0; cout<<"Enter the String: "; gets(str); len = strlen(str); cout<<"\nCharacter\tFrequency"; for(i=0; i<len; i++) { ch = str[i]; for(j=0; j<len; j++) { if(ch==str[j]) { freq++; for(k=j; k<(len-1); k++) str[k] = str[k+1]; len--; str[len] = '\0'; j--; } } cout<<endl<<ch<<"\t\t"<<freq; freq=0; i--; } cout<<endl; return 0; } Here is its sample run with user input, codes cracker dot com: In the preceding program, whenever a character matches a character at any index, we shift all the characters from that index back to one index. Note: Don't forgot to initialize a null-terminated character (\0) at the last index of the new string. Count each character and print the remaining string every timeThis program is identical to the previous one. The only difference is that this program also prints the remaining string in the third column: #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int main() { char str[100], ch; int i, j, k, len, freq=0; cout<<"Enter the String: "; gets(str); len = strlen(str); cout<<"\nCharacter\tFrequency\tRemaining String"; for(i=0; i<len; i++) { ch = str[i]; for(j=0; j<len; j++) { if(ch==str[j]) { freq++; for(k=j; k<(len-1); k++) str[k] = str[k+1]; len--; str[len] = '\0'; j--; } } cout<<endl<<ch<<"\t\t"<<freq<<"\t\t"<<str; freq=0; i--; } cout<<endl; return 0; } Here is its sample run with the same user input as provided in the previous program: The same program in different languages
« Previous Program Next Program » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |