471,612 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

character array correction

Hi there, everyone! I'm a student studying computer animation. But
alas, in order to graduate I have to take a programming class and it's
kicking my butt. I have two projects left to write (and believe me, to
some of the veterans out there, it'll be a piece of cake) one of which
takes a character array and capitalizes where necessary - eg. begining
of the string, after a period. Names and such aren't taken into
account. Like I said, very beginner level stuff. Still, I am having
trouble with the program flow.

I have a character array set up with a maximum input, and I wrote if
statements for every letter, but only for the first member of the
array.

Using a for loop, how do I generalize whether or not subsequent letters
need to be capitalized. I tried creating a bool variable to tell me
whether or not the letter in question IS a capital, but that's where I
get stuck. I'm not using the string library and I KNOW there is a more
succinct method - but that darn syntax gets me every time.

Any help would be much appreciated!!

~junk

/* fu********@gmail.com */

Feb 28 '06 #1
6 1988
I V
fu********@gmail.com wrote:
I have a character array set up with a maximum input, and I wrote if
statements for every letter, but only for the first member of the
array.
"If statements for every letter"? That sounds odd. Perhaps if you post
your code, that would help us give you more specific advice.
Using a for loop, how do I generalize whether or not subsequent letters
need to be capitalized. I tried creating a bool variable to tell me
whether or not the letter in question IS a capital, but that's where I
get stuck. I'm not using the string library and I KNOW there is a more
succinct method - but that darn syntax gets me every time.


One method to consider is using a flag variable which you can set if
the next letter should be capitalized. Here's some psuedo-code which
should explain what I mean:

if current_char is whitespace:
go on to the next character

if current_char is a letter and should_capitalize_next is set:
replace current_char with a capital

if current_char == '.':
set the should_capitalize_next flag
else:
unset the should_capitalize next flag
Hopefully trying to turn that pseudo-code into actual C++ should help.
Let us know how you get on (particularly if I haven't explained it
clearly, or you have difficulty figuring out how to write the C++
translation of the pseudo-code).

Feb 28 '06 #2
In article <11*********************@i40g2000cwc.googlegroups. com>,
fu********@gmail.com wrote:
Hi there, everyone! I'm a student studying computer animation. But
alas, in order to graduate I have to take a programming class and it's
kicking my butt. I have two projects left to write (and believe me, to
some of the veterans out there, it'll be a piece of cake) one of which
takes a character array and capitalizes where necessary - eg. begining
of the string, after a period. Names and such aren't taken into
account. Like I said, very beginner level stuff. Still, I am having
trouble with the program flow.

I have a character array set up with a maximum input, and I wrote if
statements for every letter, but only for the first member of the
array.

Using a for loop, how do I generalize whether or not subsequent letters
need to be capitalized. I tried creating a bool variable to tell me
whether or not the letter in question IS a capital, but that's where I
get stuck. I'm not using the string library and I KNOW there is a more
succinct method - but that darn syntax gets me every time.

Any help would be much appreciated!!


So, the fist character should be capitalized, that's easy right? Find
the first character, then capitalize it. (look up the function "toupper"
in the #include <cctype>. )

Then find the next period, from there find the next character and
capitalize it.

Keep going until you hit the end of the string.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 28 '06 #3
posted:
Hi there, everyone! I'm a student studying computer animation. But
alas, in order to graduate I have to take a programming class and it's
kicking my butt. I have two projects left to write (and believe me, to
some of the veterans out there, it'll be a piece of cake) one of which
takes a character array and capitalizes where necessary - eg. begining
of the string, after a period. Names and such aren't taken into
account. Like I said, very beginner level stuff. Still, I am having
trouble with the program flow.

I have a character array set up with a maximum input, and I wrote if
statements for every letter, but only for the first member of the
array.

Using a for loop, how do I generalize whether or not subsequent letters
need to be capitalized. I tried creating a bool variable to tell me
whether or not the letter in question IS a capital, but that's where I
get stuck. I'm not using the string library and I KNOW there is a more
succinct method - but that darn syntax gets me every time.

Any help would be much appreciated!!

~junk

/* fu********@gmail.com */

I'll get you started:
void ManipulateStringCharacterByCharacter(char* p_char)
{
bool capitalize_next_character = false;

for( ; *p_char; ++p_char)
{
char &current_char = *p_char;

if ( current_char == '.' )
{
capitalize_next_character = true;
continue;
}

//Here's your playground
}
}
-Tomás

Feb 28 '06 #4
In article <mq******************@news.indigo.ie>,
"Tomás" <NU**@NULL.NULL> wrote:
I'll get you started:
void ManipulateStringCharacterByCharacter(char* p_char)
{
bool capitalize_next_character = false;

for( ; *p_char; ++p_char)
{
char &current_char = *p_char;

if ( current_char == '.' )
{
capitalize_next_character = true;
continue;
}

//Here's your playground
}
}


Now I wouldn't do anything like that. I would do something more like:

char* find_letter( char* s ) {
while ( *s && ! isalpha( *s ) ) ++s;
return s;
}

char* find_period( char* s ) {
while ( *s && *s != '.' ) ++s;
return s;
}

Then...

char* p_char = str;
*p_char = toupper( *p_char );
while ( *p_char ) {
p_char = find_period( p_char );
p_char = find_letter( p_char );
if ( *p_char )
*p_char = toupper( *p_char );
}

In the process, I would show the student how to test each function
individually and when they were later introduced to std::string's find
functions, the functions would look natural.

Of course if I was the teacher, the student would learn about
std::string first thing.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 28 '06 #5
On 2006-02-28, Daniel T. <po********@earthlink.net> wrote:
In article <mq******************@news.indigo.ie>,
"Tomás" <NU**@NULL.NULL> wrote:
I'll get you started:
void ManipulateStringCharacterByCharacter(char* p_char)
{
bool capitalize_next_character = false;

for( ; *p_char; ++p_char)
{
char &current_char = *p_char;

if ( current_char == '.' )
{
capitalize_next_character = true;
continue;
}

//Here's your playground
}
}


Now I wouldn't do anything like that. I would do something more like:

char* find_letter( char* s ) {
while ( *s && ! isalpha( *s ) ) ++s;
return s;
}

char* find_period( char* s ) {
while ( *s && *s != '.' ) ++s;
return s;
}

Then...

char* p_char = str;
*p_char = toupper( *p_char );
while ( *p_char ) {
p_char = find_period( p_char );
p_char = find_letter( p_char );
if ( *p_char )
*p_char = toupper( *p_char );
}

In the process, I would show the student how to test each function
individually and when they were later introduced to std::string's find
functions, the functions would look natural.

Of course if I was the teacher, the student would learn about
std::string first thing.


It's a plain evil assignment. Its a hard problem to programmatically
capitalize arbitrary text. You need a natural language parser,
combined with an enormous dictionary of arbitrary rules. I once wrote
a program to fix the capitalization of names, which were all
uppercase, since they looked bad in a mail merge. Anybody got a manual
for how names ought to be capitalized? And that's just a sub-problem
of this assignment.

How should the following sentence be capitalized?

ed mcbain wandered around the o.p.e.c. meeting, and purchased several
copies of "the flying squirrel who ate the soup," which he passed to
to the ncos guarding his manhattan apartment.

--
Neil Cerutti
A song fest was hell at the Methodist church Wednesday. --Church
Bulletin Blooper
Mar 1 '06 #6
In article <sl**********************@FIAD06.norwich.edu>,
Neil Cerutti <le*******@email.com> wrote:
On 2006-02-28, Daniel T. <po********@earthlink.net> wrote:
In article <mq******************@news.indigo.ie>,
"Tomás" <NU**@NULL.NULL> wrote:
I'll get you started:
void ManipulateStringCharacterByCharacter(char* p_char)
{
bool capitalize_next_character = false;

for( ; *p_char; ++p_char)
{
char &current_char = *p_char;

if ( current_char == '.' )
{
capitalize_next_character = true;
continue;
}

//Here's your playground
}
}


Now I wouldn't do anything like that. I would do something more like:

char* find_letter( char* s ) {
while ( *s && ! isalpha( *s ) ) ++s;
return s;
}

char* find_period( char* s ) {
while ( *s && *s != '.' ) ++s;
return s;
}

Then...

char* p_char = str;
*p_char = toupper( *p_char );
while ( *p_char ) {
p_char = find_period( p_char );
p_char = find_letter( p_char );
if ( *p_char )
*p_char = toupper( *p_char );
}

In the process, I would show the student how to test each function
individually and when they were later introduced to std::string's find
functions, the functions would look natural.

Of course if I was the teacher, the student would learn about
std::string first thing.


It's a plain evil assignment. Its a hard problem to programmatically
capitalize arbitrary text.


Well, the assignment was to capitalize the first letter in the string,
and each letter immediately after a period. Which is a bit different,
and quite a bit simpler than properly capitalizing arbitrary text.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 2 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by dam_fool_2003 | last post: by
15 posts views Thread by ehabaziz2001 | last post: by
7 posts views Thread by arnuld | last post: by
44 posts views Thread by Kulgan | last post: by
14 posts views Thread by Shhnwz.a | last post: by
15 posts views Thread by CptDondo | last post: by
6 posts views Thread by borophyll | last post: by
13 posts views Thread by Ivan | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

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.