Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a string and need to determine the number of vowels that are included between two consonants.
Eg:
Input:Water boils at one hundred degrees.
Output:5

Explication:The only vowels that respect the rule are:
'a' and 'e' from water,'u' and 'e' from hundred and the first 'e' from degrees.

What I have tried:

#include<stdio.h>
bool vowel(char c)
{
	if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
		return true;
	else
		return false;

}
bool consonant(char c)
{
	if (c >= 'a'&&c <= 'z'&&vowel(c) == false)
		return true;
	else
		return false;
}
int main()
{
	char S[256];
	int i;
	int nr = 0;
	scanf_s("%s", S);
	for (i = 1; S[i]!=EOF; i++)
		if (vowel(S[i]) == true && consonant(S[i - 1]) == true && consonant(S[i + 1]) == true)
			nr++;
	printf("%d", nr);
	
	system("pause");
	return 0;
}
Posted
Updated 18-Aug-18 14:39pm
Comments
KarstenK 19-Aug-18 4:40am    
you dont need to check against true. The "== true" is noot needed.

What happens if you enter a capital letter?

Also, EOF is defined as -1. You actually want to look for S[i] != 0 in the for loop because in C strings are terminated with a null character, 0.

Another thing, the bool type is not defined in the C language so add a "typedef int bool;" statement in there along with macros definitions for true and false as 1 and 0 respectively.
C++
typedef int bool;

#define true  1
#define false 0
Lastly, if you want to read a line of text containing multiple words gets( S ); is a good option. It reads characters until a newline or EOF is received.
 
Share this answer
 
v3
Comments
Neri-kun 18-Aug-18 17:38pm    
First of all,i don't know what happens because i got these particular errors:
1>c:\users\dragos\source\repos\c learning process\c learning process\main.c(2): error C2061: syntax error: identifier 'vowel'
1>c:\users\dragos\source\repos\c learning process\c learning process\main.c(2): error C2059: syntax error: ';'
1>c:\users\dragos\source\repos\c learning process\c learning process\main.c(2): error C2059: syntax error: '<parameter-list>'
1>c:\users\dragos\source\repos\c learning process\c learning process\main.c(10): error C2061: syntax error: identifier 'consonant'
1>c:\users\dragos\source\repos\c learning process\c learning process\main.c(10): error C2059: syntax error: ';'
1>c:\users\dragos\source\repos\c learning process\c learning process\main.c(10): error C2059: syntax error: '<parameter-list>'
1>c:\users\dragos\source\repos\c learning process\c learning process\main.c(24): warning C4013: 'vowel' undefined; assuming extern returning int
1>c:\users\dragos\source\repos\c learning process\c learning process\main.c(24): error C2065: 'true': undeclared identifier
1>c:\users\dragos\source\repos\c learning process\c learning process\main.c(24): warning C4013: 'consonant' undefined; assuming extern returning int
1>c:\users\dragos\source\repos\c learning process\c learning process\main.c(28): warning C4013: 'system' undefined; assuming extern returning int
1>c:\users\dragos\source\repos\c learning process\c learning process\main.c(22): warning C4473: 'scanf_s' : not enough arguments passed for format string
1>c:\users\dragos\source\repos\c learning process\c learning process\main.c(22): note: placeholders and their parameters expect 2 variadic arguments, but 1 were provided
1>c:\users\dragos\source\repos\c learning process\c learning process\main.c(22): note: the missing variadic argument 2 is required by format string '%s'
1>c:\users\dragos\source\repos\c learning process\c learning process\main.c(22): note: this argument is used as a buffer size
1>Done building project "c learning process.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
However when i made this programm on c++,i found out that when for example i write "Water" with w as capital letter,'a' doesn't respect the rule.
Secondly,in c++ can EOF be defined as null character?
Rick York 18-Aug-18 18:42pm    
Your file is called main.c and the type bool is not defined in the C language. If you change bool to int it likely work better. You will also have to define true and false macros to 1 and 0 respectively.
Neri-kun 19-Aug-18 5:16am    
Yes,you're right.Instead of using bool,true and false,by using int,1 and 0 it will
most likely work better.However,how about reading the character array?If i want to define S[size] as char and read it,how can i do it ,as an equivalent of cin.getline(S,size) in C++ ,in C ?
Rick York 19-Aug-18 20:26pm    
gets will read an entire line of text until it reaches a newline or EOF. This can be handy for redirecting standard input to read from a file and processing a whole bunch of input. This would let you enter all of your test cases into a text file and then process them all at one go. This is very convenient and a common way to do things.
Patrice T 18-Aug-18 20:22pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
Quote:
However when i made this programm on c++,i found out that when for example i write "Water" with w as capital letter,'a' doesn't respect the rule.

Because your code is only lowercase, you need to add uppercase to your code.
Quote:
Secondly,in c++ can EOF be defined as null character?

Yes you can define EOF as a 0 char, just include it in your code.
C Language: #define Directive (macro definition)[^]
#define Directive (C-C++)[^]
 
Share this answer
 
few issues:
1. Your tag says C; you used bool, true, false; does not exists in C.
2. As Rick mentioned you do not use EOF; EOF=end of file. You need to check 0
3. Importantly scanf only read your first word and ignore every other words after the first space. You can use fgets, gets.
4. Also, as Rick mentioned; you should address capital letters;
 
Share this answer
 
Comments
Neri-kun 19-Aug-18 5:04am    
1.If bool,true and false don't exist,what equivalent should i use for c?
2.I get it.
3.If i define S[size] as char and i want to read the character array,is for example fgets(S,size) in C an equivalent to cin.getline(S,size) in C++?
4.I will also take the capital letters into consideration

Update:I managed to make my program work as expected,however regarding what you and Rick said,my program still works even though i used EOF.Moreover can you explain to me if there is any difference whatsoever if i write S[i]!=0 or S[i]!='\0'?
Mohibur Rashid 19-Aug-18 18:29pm    
about 1
in c 0 is false evrything else is true.
about EOF
If you debug through then you would see that your program cross to unassigned part, after 0. for a test reduce your max to 4; enter cod, your application will throw segmentation falt error and application will crash.

about 3:
may be. but in fgets you also need to assign stdin as your input

about 0 and \0, 0 is integer; \0 is telling the ompiler that is character.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900