How to convert the first character of each word to an upper

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

How to convert the first character of each word to an upper

Postby adoubrovskaia » Fri Aug 14, 2009 9:46 am

Hi,
I have an assignment to convert the first character of each word to a upper case letter in a sentence "To be or not to be";
Could you please explain to me - why?
What is wrong here
Thanks a lot,
Alla

Here is my code that is not working
#include<iostream>
#include<string.h>

using namespace std;

int main(){
int afterSpace = ' ';
char list[5000] = "To be or not to be";
char word[5000];
int j = 0;

int i = 0;

while(list[i] != '\0')
{
while(list[i] >= 'a' && list[i] >= 'z')
word[j] = list[i++];

}
if(afterSpace){
word[j] -=32;
}
else {
word[j++];}


cout <<"The new string is: " ;

cout << word[j];
return 0;
}
adoubrovskaia
 
Posts: 6
Joined: Thu Aug 13, 2009 2:15 pm

Re: How to convert the first character of each word to an upper

Postby Alvaro » Fri Aug 14, 2009 10:22 am

In the code you wrote, `afterSpace' is initialized to 32 (the ASCII code for ' ') and never changed. In the `if' statement in which it is used, it will always be interpreted as true, because it's not 0.

Also note that j looks like a loop variable, but you only change it outside of the loops.

I would scratch that code and start over, trying to solve the problem in small increments. For instance, make sure that you can write the sentence out character by character without modification. Then try to identify the first character of each word and print a '*' before it. Then do the whole thing. Compile and test your program as often as you can. That way, when you find problems, you'll be confident that certain parts are correct, and you probably introduced the problem in the last few lines you wrote.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Re: How to convert the first character of each word to an upper

Postby adoubrovskaia » Mon Aug 17, 2009 9:00 am

OK,
I started all over again
Here is my code where I was trying to print out the string "To be or not to be" back words
I compiled it. But for some reason it is not working either
Could you please take a quick look

#include<iostream>
#include<string.h>

using namespace std;

int main(){

char list[] = "To be or not to be";
char word[50];
int i;

//for(i = 0; list[i] != '\0'; ++i)
int length = strlen(list);

for(i = 0; i < length ; i++)

{
word[i] = list[length -i];
}
word[i] = '\0';
cout <<"The new string is - " << word << endl;



}
adoubrovskaia
 
Posts: 6
Joined: Thu Aug 13, 2009 2:15 pm

Re: How to convert the first character of each word to an upper

Postby Alvaro » Mon Aug 17, 2009 9:09 am

You have an off-by-one error. When i=0 your loop's body does `word[0] = list[length];', but list[length] is '\0', so `word' will now seem empty.

Try `word[i] = list[length-i-1];' instead, and you should be OK.

Why are you including <string.h>? You don't really need it. And if you ever do, you should use its newer C++ name: <cstring>.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Re: How to convert the first character of each word to an upper

Postby adoubrovskaia » Mon Aug 17, 2009 9:15 am

great!
Thanks a lot
adoubrovskaia
 
Posts: 6
Joined: Thu Aug 13, 2009 2:15 pm

Re: How to convert the first character of each word to an upper

Postby yashwanth.annem » Tue Sep 01, 2009 12:20 pm

hi
this is what i did for the given assignment

Code: Select all
#include<iostream.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>

void main()
{
clrscr();
char s[500]=" im ram laxman kumar ";
int i,j;
int n = strlen(s);
for(i=0;i<=n;i++)
{
  if(islower(s[0]))  //checking first letter for case//
  { s[0]=toupper(s[0]);
  }
  if(s[i]==' ')  //checking for space//
  {  j=i;   //assigning i value to j//
    j++;    //incrementing j to skip the spaces//
  continue;
  }
  if(islower(s[j]))  //represent the first letter of the consecutive words//
  {
  s[j]=toupper(s[j]);
  }

  }
  cout<<s;
  }
yashwanth.annem
 
Posts: 2
Joined: Mon Aug 31, 2009 10:52 am

Re: How to convert the first character of each word to an upper

Postby Alvaro » Tue Sep 01, 2009 12:28 pm

That seems very wrong... Did you test it?
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Re: How to convert the first character of each word to an upper

Postby yashwanth.annem » Fri Sep 04, 2009 7:20 am

i did check that for many cases ,and as im newbie to programming ,please let me know what's wrong with that code
thanking you,
yashwanth.annem
 
Posts: 2
Joined: Mon Aug 31, 2009 10:52 am

Re: How to convert the first character of each word to an upper

Postby Alvaro » Fri Sep 04, 2009 7:41 am

If you enable a high level of warnings (which you should), your compiler will tell you that you might be using j uninitialized (try a string that doesn't start with a space). You might not see the random crashes that your code might generate if you are using Visual C++ in debug mode.

You are checking the first letter inside the loop (over and over again).

<iostream.h> is obsolete, and main() should return an int. There are stickies somewhere in this forum explaining these things. Just read all the stickies and you'll learn a couple of things.

viewtopic.php?f=4&t=266
viewtopic.php?f=4&t=252
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA


Return to Homeworks

Who is online

Users browsing this forum: No registered users and 5 guests