we need to solve the following question.

Write a program that asks the user for a word, and then prompts a user for a letter. Your program should count the number of times the letters entered appear in the word, and then continue prompting for new letters. The program should list the letters found and number of times they appear in the word.

Sample Run:
Enter a word: hello
Enter the letter (enter zero to quit): a
There are 0 a’s
Enter the letter (enter zero to quit): h
There are 1 h’s
Enter the letter (enter zero to quit): 0

You found these letters:
h

Recommended Answers

All 13 Replies

It's quite simple, just get the string(word) into a char array,initialise int array (with all elements=0)to count occurences .Then input the required letter start a loop with i=0 and char of i not equal to /0 then check for the equality of entered letter and char of i if its true then increment the int array of j(another variable which is to be dealt with in the outer loop) after looping display the variable ;all these are to be further put in a loop(outside) in which the condition should be ......(u can guess it) and to know about the found ones run a loop and check if int array of 'k'(loop variable) is not equal to 0,.................... if any further doubts send me a private message try to do it yourself.........then only you're gonna improve

commented: that's likr a dani! +0

I hope you can code the basic part for accepting the string.The thing that you need to do next is:

void check(){   
char* cont;
char a;
int ctr=0;
cout<<"Enter the letter to be found :";
cin>>a;
for(int i=0; i<strlen(cont);i++) 
{if(cont[i]==a) {ctr++;} }
cout<<"Number of times"<<a<<"appeared-"<<ctr;

I hope you understood the basic logic.

n prompts a user for a letter. Your program should count the number of times the letters entered appear in the word, a

@Techite - don't do the program for him! He first needs to show his attempt (according to Daniweb rules) and then you help him from there!

commented: Indeed +14

In addition to what Phillamon already pointed out; this snippet is hooribly broken and very poorly formatted. You would be wise not to use it.
Start by showing us what you have so far and we'll see how we can help.

i support Philamon & Nick Evan let him try out by himself

commented: yes +0

my teacher gave me the same problem i cant do it either;[

i even dont knw where to start ...

and btw thats a girls name u keep calling him;]

My code had only one issue which was the brackets. But i gave the actual work that was requested. Then why have people negatively marked my post???

commented: you should not have given the code only just assistance +0
commented: Negative Rep on this post is actually a misuse of the rep comment system, which is not to be used as way of replying to this post. So Up-voting. +3

I hope this will help you, and I added some explanations along the source code.

/*
 * count.cpp
 * count.cpp is licensed under GNU GENERAL PUBLIC LICENSE
 *  Created on: May 15, 2012
 *      Author: sin
 */

#include <string>
#include <iostream>
#include <sstream>
using namespace std;

int main(){
    stringstream lettersfound, lettersnotfound; //stringstream used to store the found or not found letters in this program.
    string input; //the word.
    cout<<"Insert word: ";
    cin>>input; //reading the word.
    if (input!="0"){//so that we can exit the program.
        char a; //initializing the character.
        bool state; //initializing the state: it will be set to false if the character inserted had already been introduced.
        int count, tempcount; //counting the letters, and used in clearing buggs (tempcount).
        for(;;){//an infinite for. like while (true) or while(1).
            state=true; //initializing the state to true.
            count=0; //initializing count to 0.
            cout<<"Insert a letter (and 0 to exit): ";
            cin>>a; //inserting a letter.
            tempcount=count; //tempcount = count
            for (int i=0;i<(int)input.size();i++){
                if(input[i]==a){
                    count++; //counts how many times the letter introduced is in the word.
                }
            }
            if (tempcount!=count){ //if cout==tempcount => the letter wasn't found in the word. in this case we are looking if the
                                   // tempcount variable is different from count, so that the value of count changed.
                if (lettersfound.str().size()==0){ //if is the first value introduced it will be stored immediately in the lettersfound.
                    lettersfound<<a<<" ";
                    cout<<"We have found "<<count<<" letters of type "<<a<<" in the word "<<input<<".\n";
                }
                else{ // if the size of lettersfound is != 0 => we are passed the first try.
                    for(int i=0;i<(int)lettersfound.str().size();i++){ //we are searching inside lettersfound for that character. if it is
                                                                       //found than we will ignore this try.
                        if (lettersfound.str()[i]==a){
                            cout<<"You have already inserted that character %"<<a<<"%.\n";
                            state=false; //setting the state state to false.
                        }
                    }
                    if (state==true){ //if the state is true, we will add the letter.
                        lettersfound<<a<<" ";
                        cout<<"We have found "<<count<<" letters of type "<<a<<" in the word "<<input<<".\n";
                    }
                }
            }
            else if(a=='0'){ //if the character 0 is introduced it will get out.
                break;
            }
            else{
                if (lettersnotfound.str().size()==0){ //if the letter wasn't found in the word, and the lettersnotfound streamstring size
                                                      // is != 0 than it will add the letter immediately.
                    lettersnotfound<<a<<" ";
                    cout<<"We haven't found these letter "<<a<<" in the word "<<input<<".\n";
                }
                else{
                    for(int i=0;i<(int)lettersnotfound.str().size();i++){//the string.size() function will return a signed integer, so we
                                                                         //must convert it to a regular int.
                        if (lettersnotfound.str()[i]==a){//we are seaching for duplicate letters.
                            cout<<"You have already inserted that character %"<<a<<"%.\n";
                            state=false;
                        }
                    }
                    if (state==true){//if the state is true the letter will be add.
                        lettersnotfound<<a<<" ";
                        cout<<"We haven't found these letter "<<a<<" in the word "<<input<<".\n";
                    }
                }
            }
        }
        if (lettersfound.str().size()!=0){
            cout<<"I have found these letters: "<<lettersfound.str()<<endl; //printing out the result.
        }
        else{
            cout<<"Not a single correct letter was introduced.\n";
        }
        if (lettersnotfound.str().size()!=0){
            cout<<"I couldn't find these letters: "<<lettersnotfound.str()<<endl;
        }
        else{
            cout<<"Not a single incorrect letter was introduced.\n";
        }
    }
    else{
        cout<<"Invalid input.\n";
    }
    return(0);
}

My code had only one issue which was the brackets. But i gave the actual work that was requested. Then why have people negatively marked my post???

As for you a new user, you are not supposed to spoon feed any codes to the help-seeker. Because giving away a code doesn't imporve the skills of the seeker. So what you have to do is, give the the concept and other assistance & let the OP do the job of crafting a code which incoprate the concept. And have a nice time at Daniweb. CJ

As for you a new user, you are not supposed to spoon feed any codes to the help-seeker. Because giving away a code doesn't imporve the skills of the seeker. So what you have to do is, give the the concept and other assistance & let the OP do the job of crafting a code which incoprate the concept. And have a nice time at Daniweb

As for you a new user,

Any reason for copy-pasting my post?? Or any use??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.