Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 26th, 2004, 2:05 AM   #1
The_Kingpin
Newbie
 
Join Date: Oct 2004
Posts: 4
Rep Power: 0 The_Kingpin is on a distinguished road
Hi all, I need to make a function that convert a string into a certain format. Here what are the restriction:

-The first letter of the first and last name must be uppercase.
-If a first name contains only 1 character, a '.' must follow the char.
-If we find a character that isn't a letter (.&*-), we must swap it for a '/' and
add the correct spaces.

I tried making a function but my switch seems to have an error. If anyone could help me out or give me a better way, that would be very appreciated !
Right now I'm reading each letter of a name and treating itindividually. I'm pretty sure there's a better way I'm not aware of

Thanks, Frank

int formatName(char *name[], int length)
{
   int i;
   if(length==1)
   {
     /* If the word has only 1 letter, we put it in uppercase,
       add a space after the letter and resize the word to
       move every letter after the space
     */
     toupper(name[i]);
     name[i+1]=' ';
     for(i;i<length;i++)
      {
        realloc(name[length], sizeof(name[length]+1));
        name[i+1]=name[i];
      }
   }
   else
   {
     /* The word has more than 1 letter */
     for(i=0; name[i]<=name[length]; i++)
     {
        switch(name[i])
        {
          /* If we find a space, then the next character
            is a new word and starts with an uppercase. */
          case ' ':
             toupper(name[i+1]);
             break;

          /* If the character is not a letter, we swap it
            for a / and we check if there is spaces between
            it. */
          case '.': case '&': case '*':
             name[i] = '/';
             if(name[i+1]!=' ' && name[i+1]!='\0'){name[i+1]=' ';}
             if(name[i-1]!=' ' && name[i-1]!='\0'){name[i-1]=' ';}
             break;

          /* If the character wasn't treat yet, it's a normal
            case and we put it in lowercase. */
          default:
             tolower(name[i]);
        }
     }
   }
   return 0;
}
The_Kingpin is offline   Reply With Quote
Old Oct 26th, 2004, 4:04 AM   #2
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
any chance of letting us know the error its a big vauge saying there is an error :/
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old Oct 27th, 2004, 12:12 AM   #3
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
I for one can not really help you just by looking at a bunch of code on a screen, it could take a good hour to filter out any bugs related with syntactcal issue in your code.

If you could post to us what error is occuring, where the error is occuring, and what you know about the conditions of that error it would make the process much more reasonable, more people will probably stop to look at the code too
__________________
Clifford Matthew Roche &lt;geek@cliffordroche.com&gt;
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Oct 27th, 2004, 4:38 AM   #4
jasper_ferrer
Newbie
 
Join Date: Sep 2004
Posts: 23
Rep Power: 0 jasper_ferrer is on a distinguished road
int i;
   if(length==1)
   {
     /* If the word has only 1 letter, we put it in uppercase,
       add a space after the letter and resize the word to
       move every letter after the space
     */
     toupper(name[i]);
     name[i+1]=' ';
   ...
   }

Quote:
if the word has only 1 letter, we put it in uppercase...
ok...

Quote:
...resize the word to move every letter after the space
i thought there is only only 1 letter?

here's one that really stands out:

touuper(name[i]);

you used i without initializing it. ok, that's just a quick glance. i'll leave the rest to you guys.
jasper_ferrer is offline   Reply With Quote
Old Oct 27th, 2004, 10:23 AM   #5
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 6 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
#include <cctype>

void charToLower(char blah[]) {
  for(int i=0;i<sizeof(blah)/sizeof(blah[0]);i++) blah[i] = tolower(blah[i]);
}

This function will allow you to send a character array to it and automatically convert to lowercase. Remember to include cctype.
__________________

tempest is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:54 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC