From c++ string to c

P

Prof.Stanley

hello , i ma new in this group and writing over the fact that i have a
small project at hand,i am writing a program which can permit the input
of a text of arbitary lenght,and the number of double vowels must be
determined.
Output:frequency of each double vowel ,must be dtermined.

e.g.
Later ,we will see you again

OUTPUT:1xee,1xou,1xai.

I already wrote one but using the new library string type string,but i
wish to do this for a particular purpose and hence wish to use rather
the c string and functions from <string.h>,
e.g strcpy,....but i am also very new in c so i have not understood how
i can convert the code in that way, i will post the code here and
please welcome any new ideas.

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

void scanner(string);
bool isVowel(char);

int main(){
string sentence;
cout << "Enter $ to terminate!" <<endl;
do{
cout << "\nEnter the input string: ";
getline(cin,sentence);
scanner(sentence);
}while(sentence != "$");
return 0;
}

void scanner(string s){
int stringSize = s.length();
char current,next;
int count = 0;
int aa,ae,ai,ao,au,ea,ee,ei,eo,eu,ia,ie,ii,io,iu,oa,oe,oi,oo,ou,
ua,ue,ui,uo,uu;
aa = ae = ai = ao = au = 0;
ea = ee = ei = eo = eu = 0;
ia = ie = ii = io = iu = 0;
oa = oe = oi = oo = ou = 0;
ua = ue = ui = uo = uu = 0;
while(count < stringSize){
current = s[count];
if( (count + 1) == stringSize)
break;
next = s[count + 1];
if(isVowel(current) && isVowel(next)){
//a
current = tolower(current);
next = tolower(next);
if( current == 'a' && next == 'a')
aa++;
else if( current == 'a' && next == 'e')
ae++;
else if( current == 'a' && next == 'i')
ai++;
else if( current == 'a' && next == 'o')
ao++;
else if( current == 'a' && next == 'u')
au++;
//e
else if( current == 'e' && next == 'a')
ea++;
else if( current == 'e' && next == 'e')
ee++;
else if( current == 'e' && next == 'i')
ei++;
else if( current == 'e' && next == 'o')
eo++;
else if( current == 'e' && next == 'u')
eu++;
//i
else if( current == 'i' && next == 'a')
ia++;
else if( current == 'i' && next == 'e')
ie++;
else if( current == 'i' && next == 'i')
ii++;
else if( current == 'i' && next == 'o')
io++;
else if( current == 'i' && next == 'u')
iu++;
//o
else if( current == 'o' && next == 'a')
oa++;
else if( current == 'o' && next == 'e')
oe++;
else if( current == 'o' && next == 'i')
oi++;
else if( current == 'o' && next == 'o')
oo++;
else if( current == 'o' && next == 'u')
ou++;
//u
else if( current == 'u' && next == 'a')
ua++;
else if( current == 'u' && next == 'e')
ue++;
else if( current == 'u' && next == 'i')
ui++;
else if( current == 'u' && next == 'o')
uo++;
else /*( current == 'u' && next == 'u')*/
uu++;

count++;
}
else{
count++;
continue;
}
}
int totalVowels = aa + ae + ai + ao + au +
ea + ee + ei + eo + eu +
ia + ie + ii + io + iu +
oa + oe + oi + oo + ou +
ua + ue + ui + uo + uu ;
if(totalVowels == 0){
cout << "No double vowels!" <<endl;
}
if(aa > 0){
cout << aa <<"x "<<"aa";
totalVowels -= aa;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ae > 0){
cout << ae <<"x "<<"ae";
totalVowels -= ae;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ai > 0){
cout << ai <<"x "<<"ai";
totalVowels -= ai;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ao > 0){
cout << ao <<"x "<<"ao";
totalVowels -= ao;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(au > 0){
cout << au <<"x "<<"au";
totalVowels -= au;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ea > 0){
cout << ea <<"x "<<"ea";
totalVowels -= ea;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ee > 0){
cout << ee <<"x "<<"ee";
totalVowels -= ee;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ei > 0){
cout << ei <<"x "<<"ei";
totalVowels -= ei;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(eo > 0){
cout << eo <<"x "<<"eo";
totalVowels -= eo;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(eu > 0){
cout << eu <<"x "<<"eu";
totalVowels -= eu;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ia > 0){
cout << ia <<"x "<<"ia";
totalVowels -= ia;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ie > 0){
cout << ie <<"x "<<"ie";
totalVowels -= ie;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ii > 0){
cout << ii <<"x "<<"ii";
totalVowels -= ii;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(io > 0){
cout << io <<"x "<<"io";
totalVowels -= io;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(iu > 0){
cout << iu <<"x "<<"iu";
totalVowels -= iu;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(oa > 0){
cout << oa <<"x "<<"oa";
totalVowels -= oa;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(oe > 0){
cout << oe <<"x "<<"oe";
totalVowels -= oe;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(oi > 0){
cout << oi <<"x "<<"oi";
totalVowels -= oi;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(oo > 0){
cout << oo <<"x "<<"oo";
totalVowels -= oo;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ou > 0){
cout << ou <<"x "<<"ou";
totalVowels -= ou;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ua > 0){
cout << ua <<"x "<<"ua";
totalVowels -= ua;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ue > 0){
cout << ue <<"x "<<"ue";
totalVowels -= ue;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ui > 0){
cout << ui <<"x "<<"ui";
totalVowels -= ui;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(uo > 0){
cout << uo <<"x "<<"uo";
totalVowels -= uo;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(uu > 0){
cout << uu <<"x "<<"uu"<<".";
totalVowels -=uu;
}
}

bool isVowel(char c){
return (c == 'a' || c == 'A' || c == 'e' ||
c == 'E' || c == 'i' || c == 'I' || c == 'o' ||
c == 'O' || c == 'u' || c == 'U') ;
}
 
O

osmium

Prof.Stanley said:
hello , i ma new in this group and writing over the fact that i have a
small project at hand,i am writing a program which can permit the input
of a text of arbitary lenght,and the number of double vowels must be
determined.
Output:frequency of each double vowel ,must be dtermined.
<snip>

This is not really responsive to your question but are you aware of state
machines? My instincts would be to implement a state machine somehow in
solving this problem, I suspect the code would be much simpler.
 
B

benben

This is not really responsive to your question but are you aware of state
machines? My instincts would be to implement a state machine somehow in
solving this problem, I suspect the code would be much simpler.

A state machine? Just for capturing double vowels?

I do think OP's code needs significant clean up but a state machine is
an overkill.

As for OP's question: even though it is topical in this group I suspect
you'd get much better answers from comp.lang.c because after all, we are
too close to std::string for too long :)

Regards,
Ben
 
J

Jerry Coffin

@j33g2000cwa.googlegroups.com>, stanley.a.mungwe@uni-
oldenburg.de says...
hello , i ma new in this group and writing over the fact that i have a
small project at hand,i am writing a program which can permit the input
of a text of arbitary lenght,and the number of double vowels must be
determined.
Output:frequency of each double vowel ,must be dtermined.

e.g.
Later ,we will see you again

OUTPUT:1xee,1xou,1xai.

I already wrote one but using the new library string type string,but i
wish to do this for a particular purpose and hence wish to use rather
the c string and functions from <string.h>,
e.g strcpy,....but i am also very new in c so i have not understood how
i can convert the code in that way, i will post the code here and
please welcome any new ideas.

I see no advantage of a C string over a C++ string in
this case. Then again, you're using the string little
enough that it hardly makes a real difference either way.

[ ... ]
if( current == 'a' && next == 'a')
aa++;
else if( current == 'a' && next == 'e')
ae++;

[ more elided ]

This does not strike me as a particularly efficient
method of doing the job.

I think I'd use something like this:

static char vowels[] = "aeiouy";
const int size = sizeof(vowels);

// Here we depend on the fact that the sizeof a
// string literal is one more than the number of
// characters it contains.
int counts[size][size];

// return 0 to indicate "not a vowel"
// return position among vowels to
// indicate a vowel -- i.e. a=1, e=2, etc.
//
int VowelPos(int ch) {
return strchr(vowels, ch)+1;
}

void scan(char *s) {
int first = 'x'; // must initialize to a non-vowel
int second;
int x, y;

while ((second=*++s)!='\0') {
x = VowelPos(first);
y = VowelPos(second);

++counts[x][y];
first = second;
}
}

Okay, the idea here is fairly simple: instead of using a
separate variable for each count, we use a square matrix.
To keep things a bit simpler, we include an extra row and
an extra column in the matrix. We use those to hold the
counts of any pairs that include any non-vowels
(consonant, punctuation, space, etc.) This saves us from
testing whether things are vowels inside of our loop. In
the loop, we simply read in characters, find their
position among the vowels (or 0 if it's not a vowel) and
increment the appropriate position in our matrix. Each
pair of vowels has a position in the matrix. For your
example above, the resulting matrix would look about like
this:

counts:
a e i o u y
a 0 0 1 0 0 0
e 0 1 0 0 0 0
i 0 0 0 0 0 0
o 0 0 0 0 1 0
u 0 0 0 0 0 0
y 0 0 0 0 0 0

For the moment, I've skipped over column 0 and row 0,
since we're going to ignore them anyway (that's where we
put the counts of anything that included a consonant).

To print out our results, we can use a nested loop,
something like this:

// ignore column 0 and row 0, since that's where we
// put the counts of non-vowels.
for (x=1; x<size; x++)
for (y=1; y<size; y++)
if (counts[x][y] != 0)
std::cout
<< vowels[x-1]
<< vowels[y-1]
<< "\t"
<< counts[x][y];

As for reading the data into a C-style string instead of
a C++ string, the easiest way is probably to use
ifstream::getline, something like:

const int max = 4096;
char sentence[max];

cin.getline(sentence, max);
if (sentence[0] != '$')
scan(sentence);
 
J

Jerry Coffin

Jerry said:
Overkill!

[aeiouy][aeiouy] is overkill? I didn't even need to think about it to type
that in?!

That recognizes the right string, but from there you need
to store the data correctly as well. Admittedly, most
things that support REs also have some sort of
associative array as well. If you were writing this in
AWK or PERL or something on that order, this would almost
certainly be a reasonable way to do things.

OTOH, the OP was talking about doing this in C++, which
doesn't (yet) have any support for REs. In addition, the
OP was (for whatever reason) interested in using C
instead of C++ strings. Using std::map and the regular
expressions out of (say) the once and future TR1 seems
pretty much directly counter to the sort of direction he
seems to want to go.

Given C++ as a starting point: yes, it's overkill.
 
P

Phlip

Jerry said:
Given C++ as a starting point: yes, it's overkill.

In some situations, a regex is already available, and in others it is not.

So the reason "C++ sucks" is not a good answer to "why not use a Regex?"

However, the question I answered was whether a state table was overkill.
Because a regex _is_ a state table generator, in a neat little package
behind a useful interface, then it may indeed be slightly less overkill
than writing a state table from scratch!
 
D

Default User

benben said:
A state machine? Just for capturing double vowels?

I do think OP's code needs significant clean up but a state machine
is an overkill.

As for OP's question: even though it is topical in this group I
suspect you'd get much better answers from comp.lang.c because after
all, we are too close to std::string for too long :)

He'd have to rewrite the entire program, including all the output
statements that use <iostream> and such before he could post it to clc.
While not popular, the old-style strings are still valid C++. I'll try
to give him some tips directly.



Brian
 
D

Default User

Prof.Stanley said:
hello , i ma new in this group and writing over the fact that i have a
small project at hand,i am writing a program which can permit the
input of a text of arbitary lenght,and the number of double vowels
must be determined.

I'm going to go over the program exhaustively, but I'll give you some
tips.

The first problem is declaring a string and reading into it. You either
have to pick size at the start or use dynamic memory to expand the
string based on the input.

For a first pass, I suggest choosing a fixed size and going with that.
If that's not satisfactory, then later add dynamic strings.

int main(){
string sentence;

char sentence[256]; // magic numbers are bad, you fix it
cout << "Enter $ to terminate!" <<endl;
do{
cout << "\nEnter the input string: ";
getline(cin,sentence);

fgets(sentence, sizeof sentence, stdin);

At this point, up to 255 characters could be read into sentence. That
may include a terminating '/n'. You have to decide whether the newline
is a problem, if so, deal with it.

If more characters were entered, then there will be some left hanging
out in the buffer. You decide whether to discard them or handle them
with a subsequent pass.
scanner(sentence);
}while(sentence != "$");

The comparison operators don't work (at least not the way they do with
std::string. Use:

}while(strcmp(sentence, "$") != 0)

That terminating newline is likely to be a problem.
return 0;
}


void scanner(string s){
int stringSize = s.length();

The way to get the string length isL
strlen(s);

I recommend using size_t rather than int for stringSize.
char current,next;
int count = 0;
int aa,ae,ai,ao,au,ea,ee,ei,eo,eu,ia,ie,ii,io,iu,oa,oe,oi,oo,ou,
ua,ue,ui,uo,uu;
aa = ae = ai = ao = au = 0;
ea = ee = ei = eo = eu = 0;
ia = ie = ii = io = iu = 0;
oa = oe = oi = oo = ou = 0;
ua = ue = ui = uo = uu = 0;
while(count < stringSize){
current = s[count];
if( (count + 1) == stringSize)
break;
next = s[count + 1];

The character access should be the same. Try this much see how it goes.




Brian
 
P

Prof.Stanley

Thanks very much Jerry, i think i will try your advice and in case of
anything i will write again, thanks to Brain too for your contribution,
i am also grateful to all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

input of a string 3
SYNTAX AND SEMANTICS 6
SYNTAX AND SEMANTICS 6
help needed c++ 5
Need to add a class 5
Possible bug with Range#include? 6
help needed in c++ 1
help to convert this fortan program to c++ 2

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top