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? :)

By using your brain and thinking about the problem rather than asking how it is done.

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?

Edited 6 Years Ago by thelamb: n/a

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

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;
}

Edited 6 Years Ago by empror9: n/a

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.

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.

#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;
  }
Comments
Don't give away the code

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

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

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

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

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

Comments
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.

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 !

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

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"
Return to the operating system.
End program

Before jumping ahead into arrays, you should consider using the standard class "string"(std::string). Strings are sequences of characters, with support for special operations such as retrieving/placing/replacing characters.

This article has been dead for over six months. Start a new discussion instead.