simple question
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? :)
empror9
Junior Poster in Training
95 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
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
Junior Poster in Training
95 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
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;
}
empror9
Junior Poster in Training
95 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
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
Junior Poster in Training
95 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
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
Junior Poster in Training
95 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
thanks a lot caut_baia but i can't understand the code because i'm still beginer
empror9
Junior Poster in Training
95 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
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
Junior Poster in Training
95 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0