## empror9

see this code with the queastion

``````#include <iostream>
using namespace std;
int main()
{
char str[30];
cout << "enter a word: " << endl;
cin >> str;

// if the user input: malaysia
// so the output will be 3 because there are three charchter of 'a'

return 0;
}``````

how can i do it? :)

## thelamb 163

As with any problem, try to break it up into smaller, easier pieces.
There is a string and you want to count the number of a's... in the real world, how would you do this if you had written the word on a piece of paper? Would you take a picture of the piece of paper and analyse the handwriting? Or would you simple read through the word, increasing a counter every time you see an a?

## empror9

hi,

the answer is the second one , i tried to do this but i don't know much about array and i read about array but i cloudn't do it

## empror9

i tried this one

``````#include <iostream>
using namespace std;
int main()
{
int count = 0;
char str[30];
cout << "enter a word: " << endl;
cin >> str;
char a = str[30];
while(a == 'a'){
count ++;

cout << count << endl;
}
// if the user input: malaysia
// so the output will be 3 because there are three charchter of 'a'

return 0;
}``````

## thelamb 163

Think about what you are doing. What you have right now makes no sense. You are creating a char array called 'a', then you say while a == 'a' you increase the count. But the a variable is never filled, it is just random data.

Think again what you do in real life. You first look at the first character of str, you look if it is an 'a', then you go to the second character of str, and look if it is an 'a'. You do that until you reach the end of str.

## chiwawa10 77

Why not think about the logic. Do not worry too much about the syntax and things. Try to present your logic here and I believe many of us here are more than happy to give you guidance in turning the logic into codes.

## caut_baia 9

``````#include <iostream>
using namespace std;

int char_counter(char*,char);

int main ()
{
cout << char_counter("string_literal",'i');
}

int char_counter(char* str,char c)
{
int num=0;
for (;*str!='\0';*str++)
{
if (*str==c) num++;
}
return num;
}``````
commented: Don't give away the code -1

## empror9

hi,

ok i think to do these steps

1 - use for loop and if statment to check for each elements of the array if equals to 'a'

i think that's all

i'm trying rightnow to do it

## empror9

wow i do it but the output is not clear

``````#include <iostream>
using namespace std;
int main()
{
int count = 0;
char str[30];
cout << "enter a word: " << endl;
cin >> str;
char a = str[30];
for (int x = 0; x <=30; x++)
if(str[x] == 'a'){
count++;
cout << count;
}
// if the user input: malaysia
// so the output will be 3 because there are three charchter of 'a'

return 0;
}``````

i'll try one more time

## empror9

thanks a lot caut_baia but i can't understand the code because i'm still beginer

## caut_baia 9

Well i'll explain it for a bit.

``char* str``

declares a pointer to a null terminated string '\0'.So wherever str begins it ends with a '\0'.
Imagine *str as the beggining letter of the string str.If str="1234" *str will point to the letter '1' thus by incrementing *str you are actually parsing the whole string.While *str++ , *str will point to 1 ,2 ,3 ,4 and eventually '\0' becasue that's where the string ends.That's the most of it.Good luck

## kjeka -2

I'm a newb so don't take my word for it but think:

``````int count = 0;
for(i = 0; str[i]; i++){
if (str[i] == 'a'){
count++;
}
}
cout << count;``````

will do it

commented: and as a noob, you should not do other people's homework for them. You don't get the grade, and they won't deserve it. -2

## thelamb 163

Ok you are on the right track empror.

The output you see is something like:
1234 etc. ?

Note that you have the "cout" inside the for loop, so it will be executed in every iteration of the for loop.
If you put it behind the } of the for loop, it will only output once.
If you want the next output to start on a new line, you need to write << "\n" behind the 'cout << count'

Secondly, your for loop goes from 0 up to _and including_ 30. and the variable str is an array of 30 elements.

So it starts:
str[0] // 1st element
str[1] // 2nd element
str[28] // 29th element
str[29] // 30th element
str[30] // error! str has no element 31 !

## empror9

thank you he lamb, this is my code and it works correctly

``````#include <iostream>
using namespace std;
int main()
{
int count = 0;
char str[30];
cout << "enter a word: " << endl;
cin >> str;
for (int x = 0; x <=30; x++)
if(str[x] == 'a'){
count++;

}
cout << count;
// if the user input: malaysia
// so the output will be 3 because there are three charchter of 'a'

return 0;
}``````

now, i want more challenging, i'm trying to count how many character the input by the user

## batchprogram

empror9, You really should consider reading about psuedocode, practicing writing simple programs.
When trying to write code, you should know how to convert said code into english to some extent.

Let's analyze some code and convert it into english.

``````#include <iostream.h>
int main(){
cout << "This is text displayed with cout";
return 0;
}``````

(English)
Include standard input/output streams

Start program
Output message "This is text displayed with cout"