0

It's my home work, our professor wants us to use cin to get a string first, and output the number of "a" and "c" in that string.

My idea is to delete all other letters first, for example, if you have a sting str = "apple", and you want "p"s, you delete other letters, and get a new sting which is str1 = "pp", than use str.size(), to get how many “p”s.

My problem is how can I delete other letters.

2
  • 1
    Here is a list of operations you can perform on a std::string: cplusplus.com/reference/string/string which should help with your task. Note that the list also highlights other things you could do instead Commented Oct 7, 2015 at 5:33
  • It would be faster not to delete the other characters. Make a counter variable, initialize it to 0, and then iterate over the string and add to the counter variable every time the current character is 'a' or 'c' Commented Oct 7, 2015 at 6:25

7 Answers 7

4

If you're only counting a single specific letter there is a standard library algorithm.

int p_count = std::count(str.begin(), str.end(), 'p'); 

There is a related algorithm that accepts a predicate for more complicated uses:

int ac_count = std::count_if(str.begin(), str.end(), [](char ch){ return ch == 'a' || ch == 'c'; }); 

And for yet another solution, a simple array. This is fast and counts all letters in a single pass.

int counts[256] = {}; for (unsigned char ch : str) { ++counts[ch]; } cout << "a count is " << counts['a'] << '\n'; cout << "c count is " << counts['c'] << '\n'; 
Sign up to request clarification or add additional context in comments.

3 Comments

"use cin to get a string first, and output how many "a" and "c" in that string..."
@juanchopanza: Added std::count_if example. To be honest, for this homework I'd probably just create int counts[256]; and iterate over the string, updating counts in a single pass.
Agreed. The array is probably the best solution.
2

If you are allowed to use std::map, you can use:

std:map<char, int> charCount; for (auto c : str ) { charCount[c]++; } // Number of times 'a' is found: int aCount = charCount['a']; // Number of times 'c' is found: int cCount = charCount['c']; 

1 Comment

I dont think we are able to use std::map
1

Why even delete the characters? You can achieve your goal like this:

std::string str; std::cin >> str; int a_counter = 0; for(char& c : str) { if (c == 'a') a_counter++; } std:: cout << a_counter; 

Comments

1

The C++ Standard Library has a function for this: count:

int a_count = std::count(str.begin(), str.end(), 'a'); 

Comments

0

Most of the other answers are great, and I strongly urge you to use Blastfurnace's solution; however, if you truly wanted to erase the chracters from a std::string except for a certain set of characters (say 'a' and 'c') you could use the erase-remove idiom:

std::string s; std::cin >> s; s.erase(std::remove_if(s.begin(), s.end(), [](char c){ return c != 'a' || c != 'c'; }), s.end()); cout << s.size() << endl; 

There's an answer here that explains further (disclaimer: it's my answer)

Comments

0

You can use the following thought process in solving similar problems in the future.

Programming can be simplified into performing 3 steps:

1) input data 2) process data 3) output data

Steps:

1) Understand what you are trying to solve at your input.

you understood that you have to read in input using "cin" and store it using a string variable and then count the number times a character appears in that string.

2) Learn more about the variable type/class that you are using to store your input.

In your case you are storing your input in a "string", which is of a string class/type. Go to C++ reference website to read about string class and familiarize yourself with all the functions and attributes that string class provides. See the following link: In there you will see, that string class has "[ ]" operator that returns a character. You can click on that link and see the example on how to use it.

Next, use the information you just gained to process your data.

3) Now implement your logic to process data.

In your case, you can run a for loop:

std::string str; std::cin << std::str; int counter = 0; for(int i=0; i< str.size();i++) { if( str[i] == 'p' ) counter++; } 

The if block checks every character in the string and matches it with 'p' character. If it matches, counter variable is incremented by 1. At the end, the value of "counter" is the number of times "p" appeared in the string "str"

Comments

0
for(int i=0;i<str.size();i++) { if(str[i]=='a') countOfa++; else if(str[i]=='c') countOfc++; } cout<<"coun of A is :"<<countOfa<<endl; cout<<"coun of C is :"<<countOfc<<endl; return 0; } 

1 Comment

you are welcome and also initialize countOfa and countOfc to 0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.