try to make code with this with using only...

try to make code with this with using only:
if/if...else
for
while
do while

dont use:
printf()
scanf()

here's the problem:

in C++ to determine how many of the characters are vowels and how many are consonants in a given line of text. Also terminate the string when the input character encountered is other than the alphabets(a-z or A-Z) and Blank spaces.

[Hint:(a) When the input string is 'C FOR SWIMMERS, TEST YOUR C PROGRAMMING STRENGTHS'. Consider the string 'C FOR SWIMMERS' only Because ',' is encountered. (b) When the input string is 'Y2K PROBLEM'. Consider the character 'Y' only Because the '2' is encountered.]

i have the code. so that it would be easy. but i used printf & scanf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* Counting vowels and consonants in a given line of text */
#include<stdio.h>
#include<string.h>
int main()
{
	char str[80];
	int i,vow=0,cons=0;
	printf("Enter a string : ");
	scanf("%[ ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]",str);
	for (i = 0; i < strlen(str); i++)
		if (str[i] == 'A' || str[i] == 'E'  || str[i] == 'I' ||  str[i] == 'O' || str[i] == 'U' || str[i] == 'a' || str[i] == 'e'  || str[i] == 'i' ||  str[i] == 'o' || str[i] == 'u')
			vow++;
		else if (str[i] != ' ') // Ignore BLANK characters
			cons++;
	printf("\n\nThe given string is %s",str);
	printf("\n\nThe total number of VOWELS in a given string is %d",vow);
	printf("\nThe total number of CONSONANTS in a given string is %d",cons);
    return 0;
} /* End of Main */
Use std::cout instead of printf().

Use std::cin instead of scanf(). (Read in single characters, and continue reading until newline or non-letter, non-space found).


but hey. can u teach me what the use of printf() and scanf()
because i dont get it like this one.
" scanf("%[ ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]",str); "

how can i used ' cin>> ' ?

or can make some code using cout<< and cin>> of this?
and i'll just review it.
use std::cout<<"enter your text here";
and std::cin>>variable
how about this one?

if (str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U' || str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u')

what's ' str[i] '
what is the used of that?

and

use std::cout<<"enter your text here";
and std::cin>>variable

what is std?

i should only use <iostream.h>
1
2
3
#include <iostream>

using namespace std;

that is what you need to use cout << and cin >>
It's an array. A string is a list of characters that, together, form some group of characters, hence a string. Subscripting the string cycles through the characters that form it.
I don't know why anybody uses the C IO anymore. Tutorial for iostream: http://www.cplusplus.com/doc/tutorial/basic_io/
The std:: is the namespace. You are using antiquated <.h> files which is why you don't recognize it. Some time back, all the standard headers were moved to the format <something> rather than <something.h> or "something.h". To differentiate these, all the stuff was moved into the std namespace. To qualify and access the name, you either type std:: before every use of it, going into namespace scope, or you can perform a using declaration,
using namespace std;
which brings all the names and identifiers of a namespace into global scope.
Since the previous poster got here just before me, be aware of the cause for the using directive rather than just blindly "using" it (NOT INTENTIONAL PUN, I am truly contrite). Such activity is what causes you to miss this stuff.
And you do not use <iostream.h> unless you're using some compiler from the 1600s. You use <iostream>. That's why you need std::.
Last edited on
i just finish my code here try it. thanks to all your help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<string.h>
#include<iostream.h>
int main()
{
	char str[80];
	int i,vow=0,cons=0;
	cout<<"Enter a string : ";
	cin>>str;

	for (i = 0; i < strlen(str); i++)
		if (str[i] == 'A' || str[i] == 'E'  || str[i] == 'I' ||  str[i] == 'O' || str[i] == 'U' || str[i] == 'a' || str[i] == 'e'  || str[i] == 'i' ||  str[i] == 'o' || str[i] == 'u')
			vow++;
		else if (str[i] != ' ') // Ignore BLANK characters
			cons++;

	cout<<"\n\nThe given string is "<<str;
	cout<<"\n\nThe total number of VOWELS in a given string is "<<vow;
	cout<<"\nThe total number of CONSONANTS in a given string is "<<cons;

	 return 0;
} /* End of Main */
Like I just said above, stop using iostream.h. It's antiquated.
thanks. but i have new problem.
when i run it. and i input 123ABC
it did not terminate it should be terminate the program, but it didn't.
Man I like your program a lot I would of never thought of this... Very nice.... I am trying to fix your problem but I'm stuck
ecomoda wrote:
when i run it. and i input 123ABC it did not terminate
???

There's no construction in your program which terminates it if "123ABC" is entered.

It simply writes:

The total number of VOWELS in a given string is 1
The total number of CONSONANTS in a given string is 5

because it counts anything except AEIOUaeiou and ' ' as consonants.

BTW, y (Y) is also a vowel.
BTW, y (Y) is also a vowel


But isn't it a,e,i,o,u and sometime y and w
Last edited on
But isn't it a,e,i,o,u and sometime y and w

OK. English is not my native language hence the error.

If anyone is interested in studying vowels:
http://en.wikipedia.org/wiki/Vowel#Written_vowels
Give you a tip wikipedia anyone can edit it so it not a 100% right I been learning english ever since first grade true me it a,e,i,o,u and sometime y, and w...

Here is an example of why wikipedia is not always right. Here is a wikipedia site that conflicts with your wikipedia site

http://simple.wikipedia.org/wiki/Vowel

just show that anyone can edit wikipedia
Last edited on
? w is never a vowel in English.

Also:

- Include <iostream> not <iostream.h>
- use std::string instead of a char array (your program will explode when the user enters more than 79 characters). Or better yet just use a single char and read 1 char at a time instead of trying to read the whole string at once (as jsmith suggested)
why <iostream> only? not <iostream.h>?
i dont understand " use std:: " T.T
why <iostream> only? not <iostream.h>?


tummychow wrote something about that in this thread: http://www.cplusplus.com/forum/beginner/20646/#msg108558

I'm finding his post hard to follow, but it might help. I think he's saying that <iostream> is the better way to do it. It uses a more recently updated header file (?)

i dont understand " use std:: " T.T


You can either do this:

1
2
3
std::cin
std::cout
std::endl


Or you can do this:

1
2
3
4
5
using namespace std;

cin
cout
endl
as liero said read tummychow's post earlier in this topic.

if you want to quit the program with 123ABC or something else put the entire for loop into a while loop like this:
1
2
3
4
while(str != '123ABC')
{
the rest of your code.
}
Topic archived. No new replies allowed.