Divide Strings into Char

Hi! For an assignment, I have to write a code that has several functions. One of them is counting the amount of x letters in the string. Example:

string phrase="The amount of cookies in the box is unknown";
char findLetter='a';

output should be=1.

_____________________________________________________________________________
I know how to divide the string into words, but just can't find a way to divide the word into characters to scan it.

Thanks!

Edir: I found myself a solution while experimenting, won't delete so if someone else needs this information they can have a guide.

Just went:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

int main(){
string phrase[50]="The amount of cookies in the box is unknown";
char findLetter='a';
int contador=0;

for(int i=0;i<50;i++){
   if(phrase[i]==findLetter){
         contador++;
   }
}
cout<<contador;
return 0;
}
Last edited on
line 3 should not be an array.

consider:
1
2
3
4
5
6
7
8
9
int main()
{
  string phrase{"The amount of cookies in the box is unknown"};
  char findLetter={'i'};
  int contador=0;
  for(char &c:phrase)
    {contador+= c==findLetter;}
  cout<<contador;
}   
Last edited on
I see, thanks! I got a question on your code: I'm assuming that line 4 scans the whole array in a more efficient way than mine, but how does it work? I just don't understand the sytax of what's inside of the parenthesis, it's the first time I see something like that.

For what I can see, char &c gets the character from phrase (the character is stored at c right?), but I'm not sure aboutt what is telling the loop to stop, does it stop when the string is done?

Thanks!
The for loop in line 6 is a "range-based for loop." AKA a "for-each loop."

https://www.learncpp.com/cpp-tutorial/for-each-loops/

The loop knows when to stop when the last element of the string is accessed.
With a std::string there are three types of loops that can be used to access the elements:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>

int main()
{
   // create a test string using "uniform initialization"
   // https://mbevin.wordpress.com/2012/11/16/uniform-initialization/
   std::string str { "This is a string." };

   // access each element using a "regular" for loop
   // the string can be modified
   for (size_t i { }; i < str.size(); ++i)
   {
      std::cout << str[i] << ' ';
   }
   std::cout << "\n\n";

   // using string iterators and auto type inference
   // https://www.learncpp.com/cpp-tutorial/b-2-long-long-auto-decltype-nullptr-and-enum-classes/
   // cbegin and cend are const iterators,
   // so the string can't be modified in the loop
   for (auto itr { str.cbegin() }; itr != str.cend(); ++itr)
   {
      std::cout << *itr << ' ';
   }
   std::cout << "\n\n";

   // for-each loop and const auto&
   for (const auto& itr : str)
   {
      std::cout << itr << ' ';
   }
   std::cout << '\n';
}
char &c is a reference to each letter in turn. if you leave the & off it makes a copy of each letter instead, less efficient esp when you start using objects instead of letters.

line 4 is just find-letter, same as yours, the char it looks for.
the for loop is for every letter in the string, referenced by c to access it, as noted above.
what tells it to stop is hidden. it can use the string's known length field, or iterators, or probably even the zero terminal character ... you don't need to know how it knows or which it uses, just that it works, like using a library or other people's code.

Its not really any more efficient or anything special. Just introducing you to the syntax while correcting the array mistake. The only thing really interesting in there (beyond syntax you have not seen before) is the exploitation of boolean. Boolean expressions are ensured to be 0 or 1, so adding up the trues from the expression is a little shorter than having the extra if statement. It can also be faster, depends on the code and hardware, but here we don't care that much about speed. The majority of cpus are better at doing integer math than branching.


Last edited on
Topic archived. No new replies allowed.