954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Changes the Case of Output Function

I have to write a function that takes an input such as this chevrolet, MALIBU and makes chevrolet into CHEVROLET and MAILIBU into Malibu I am having no trouble with capitalizing the CHEVROLET but when it comes time for the Malibu to be corrected it doesn't come out right.

directions if i wasnt clear: http://www.cs.niu.edu/~abyrnes/csci240/pgms/240pgm10.htm

Here is where I am at with the code:

void fixCarInfo (char *c)
{
     while (*c != ',')
     {
     *c = toupper(*c);
     c++;
     }
     
     while (*c == ',')
      {
      c++;
      }
      *c = toupper(*c);
      c++;
lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Notice that in all the input you are getting from the file,
You have an extra " " space after the "," (Comma). So I think that the code should actually be


[_CODE REMOVED_]

Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
 

>>Here is where I am at with the code:
It would be better for you to follow the instruction strictly:
If the name is in the lastname, firstname format, then it needs to be changed to the firstname lastname format. To do this:

* Copy the firstname to a temporary string
* Add a space to the end of the temporary string
* Add the last name to the end of the temporary string
* Add spaces to the end of the temporary string until it is 15 characters long.
* Copy the temporary string into the passed in string argument

At this point, the name should be in the firstname lastname format. All that remains is to make sure the first letter of each name is upper case and that the others are lower case.
So first check if the string contains a ',' comma. Which can be done by modifying the line 9 to 12 So start you function by line 9 with the following algo in mind:

To Check if the string S contain a comma:
1.[initialization] Set k to 0
2.[Checking comma] Check if the the character at k S[k] is a comma? If yes: The string has a comma. If no: set k to k+1
3.[Checked all?] Determine if you have checked all the characters? If yes: the string have no comma, if no go to next step.
4.[Repeat] Go to step 2 again.

One point to note is that the condition ofwhile should also include to check if the current character is not a null '\0' ( Remember a string is terminated by a null character).
Now as the outcome of line 9-12, you will have to convert your lastname, firstname format to firstname lastname.

Then you simply need to capitalize two characters:
* The one at the very beginning of your string and
* The one just after the first space encountered.

Code the above algo and let us know what happened.

Edit: Though skydiploma was fast than me, I do not really appreciate him giving the OP the code he wanted

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

This is where I am at with that it, it gets the last names to be first but it doesn't do the capitalizing for it. In the original post this isn't the code I was talking about I was talking about the code to fix the name of the cars, but I was struggling with this one too.

void fixName ( char *n )
{
     char temp[15];
     char *spacePtr;
     spacePtr = strchr(n,' ');
     if ( spacePtr != 0 )
     {
     strcpy(temp,spacePtr+1);
     strcat(temp,", ");
     *spacePtr = '\0';
     strcat(temp, n);
     strcpy(n, temp);
     }
}
lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

My appoligies to the OP to divert the topic.
As for the FixName:
First of all you don't need to concatenate the comma but a space.( as per your assignment) as you have to fix it to firstname lastname rather than firstname,lastname
so change the line 9
Next:
Just capatalize n[0] and n[1+strchr(n,' ')] I hope you got why?

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 
My appoligies to the OP to divert the topic. As for the FixName: First of all you don't need to concatenate the comma but a space.( as per your assignment) as you have to fix it to firstname lastname rather than firstname,lastname so change the line 9 Next: Just capatalize n[0] and n[1+strchr(n,' ')] I hope you got why?

I got to to format the first name correctly Firstname by my last name the first letter is still lowercase. Here is what I used to make it used to format the firstname how would I modify this to do the lastname?

while(*n != NULL)
      {
      if(isalpha(*n)) break;
      n++;
      }
      *n = toupper(*n);
      n++;
      
      while(*n != NULL)
      {
      *n = tolower(*n);
      n++; 
      }
      n++;
lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

your first while loops searches for the first alpha char so it can be changed to uppercase.

then the second while loops changes every single remaining character to lowercase (assuming it can be changed), until it finally finds the NULL and quits.

So... instead of continuing that second while until the NULL is found, break out of that second while once another space is found. then keep repeating this pattern of capitalizing the first letter of each word as long as words are found.

note, your code as it stands is extremely fault-intolerant, and i havent even attempted to address that. because it's a whole 'nother subject. but at least youll get the basic operation figured out.


.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

your first while loops searches for the first alpha char so it can be changed to uppercase.

then the second while loops changes every single remaining character to lowercase (assuming it can be changed), until it finally finds the NULL and quits.

So... instead of continuing that second while until the NULL is found, break out of that second while once another space is found. then keep repeating this pattern of capitalizing the first letter of each word as long as words are found.

note, your code as it stands is extremely fault-intolerant, and i havent even attempted to address that. because it's a whole 'nother subject. but at least youll get the basic operation figured out.

.

How would I test for a second space would I use an if statement such as

if(*n == NULL)
lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

No,

if(*n == ' ');
Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 

Ok, this is what I have now, some of it formatted right but some didn't, any idea why that could be?

void fixName ( char *n )
{
     char temp[15];
     char *spacePtr;
     spacePtr = strchr(n,' ');
     if ( spacePtr != 0 )
     {
     strcpy(temp,spacePtr+1);
     strcat(temp," ");
     *spacePtr = '\0';
     strcat(temp, n);
     strcpy(n, temp);
     }
     while(*n != NULL)
      {
      if(isalpha(*n)) break;
      n++;
      }
      *n = toupper(*n);
      n++;
      
      while(*n != NULL)
      {
      if(*n == ' ') break;
      n++;
      }
      *n = toupper(*n);
      n++;
}
lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

What do you think the code is doing? Add some comments.

Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 
while(*n != NULL)//enters loop when there is a character
      {
      if(isalpha(*n)) break;//test to make sure character is a letter
      n++;
      }
      *n = toupper(*n);//converts to uppercase
      n++;
      
      while(*n != NULL)//while the character holds something
      {
      *n = tolower(*n);//convert to lowercase
      n++; 
      }
      n++;
lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Especially the part before. xD

Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 

That part I'm really not sure, we were giving that code in class, I was confused when we got it. Of course when I went to see my professor yesterday I was told the good ole "you'll figure it out" :\

lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Look up the functions and what they do, then put comments in there: it makes the code way more readable for yourself. If you can't figure out what a function does (which I doubt, using google and C(++) references) you can always ask.

Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You