473,395 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

From c++ string to c

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

May 17 '06 #1
11 1893
"Prof.Stanley" writes:
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.
May 17 '06 #2
> 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
May 17 '06 #3
benben wrote:
A state machine? Just for capturing double vowels?


What do you think a regular expression is? ;-)

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
May 17 '06 #4
In article <1147863772.404018.107550
@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);

--
Later,
Jerry.

The universe is a figment of its own imagination.
May 17 '06 #5
In article <nXHag.72897$_S7.24632
@newssvr14.news.prodigy.com>, ph*******@gmail.com says...
benben wrote:
A state machine? Just for capturing double vowels?


What do you think a regular expression is? ;-)


Overkill!

--
Later,
Jerry.

The universe is a figment of its own imagination.
May 17 '06 #6
Jerry Coffin wrote:
> A state machine? Just for capturing double vowels?


What do you think a regular expression is? ;-)


Overkill!


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

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
May 17 '06 #7
In article <dbIag.72902$_S7.64498
@newssvr14.news.prodigy.com>, ph*******@gmail.com says...
Jerry Coffin wrote:
> A state machine? Just for capturing double vowels?

What do you think a regular expression is? ;-)


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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
May 17 '06 #8
Jerry Coffin wrote:
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!

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
May 17 '06 #9
benben wrote:
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 :-)


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
May 17 '06 #10
Prof.Stanley wrote:
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

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
May 17 '06 #11
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.

May 18 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

467
by: mike420 | last post by:
THE GOOD: 1. pickle 2. simplicity and uniformity 3. big library (bigger would be even better) THE BAD:
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
40
by: Peter Row | last post by:
Hi all, Here is my problem: I have a SQL Server 2000 DB with various NVarChar, NText fields in its tables. For some stupid reason the data was inserted into these fields in UTF8 encoding. ...
5
by: Gregg | last post by:
Hello all, I have been banging my head over a problem that I am having reading a comma seperated file (CSV) that can contain from 1 to 10,000 records. My code snipit is as follows: **Start...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
18
by: James Radke | last post by:
Hello, We are currently using a user DLL that when working in VB 6.0 has a user defined type as a parameter. Now we are trying to use the same DLL from a vb.net application and are having...
0
by: Cleo | last post by:
Hi, I am trying to call a WebService Method written in Weblogic from VB.NET and I am getting the following error. I am using SOAP Caal s from VB.NET. Please find the wsdl file and my code. ...
3
by: Lumpierbritches | last post by:
I have an application my partner wrote that would allow an autoresponse to any Mapi compliant email that apparently in .Net won't, can someone assist me with fixing this? Here is the code: ...
6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.