By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,110 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,110 IT Pros & Developers. It's quick & easy.

Function that standardize a string with lowercase and uppercase ?

P: n/a
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

Expand|Select|Wrap|Line Numbers
  1. int formatName(char *name[], int length)
  2. {
  3. int i;
  4. if(length==1)
  5. {
  6. /* If the word has only 1 letter, we put it in uppercase,
  7. add a space after the letter and resize the word to
  8. move every letter after the space
  9. */
  10. toupper(name[i]);
  11. name[i+1]=' ';
  12. for(i;i<length;i++)
  13. {
  14. realloc(name[length], sizeof(name[length]+1));
  15. name[i+1]=name[i];
  16. }
  17. }
  18. else
  19. {
  20. /* The word has more than 1 letter */
  21. for(i=0; name[i]<=name[length]; i++)
  22. {
  23. switch(name[i])
  24. {
  25. /* If we find a space, then the next character
  26. is a new word and starts with an uppercase. */
  27. case ' ':
  28. toupper(name[i+1]);
  29. break;
  30.  
  31. /* If the character is not a letter, we swap it
  32. for a / and we check if there is spaces between
  33. it. */
  34. case '.': case '&': case '*':
  35. name[i] = '/';
  36. if(name[i+1]!=' ' && name[i+1]!='\0'){name[i+1]='
  37. ';}
  38. if(name[i-1]!=' ' && name[i-1]!='\0'){name[i-1]='
  39. ';}
  40. break;
  41.  
  42. /* If the character wasn't treat yet, it's a normal
  43. case and we put it in lowercase. */
  44. default:
  45. tolower(name[i]);
  46. }
  47. }
  48. }
  49. return 0;
  50. }
  51.  
Nov 14 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Jim
On Tue, 26 Oct 2004 02:03:23 -0400, "The_Kingpin"
<th**********@hotmail.com> wrote:
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

Expand|Select|Wrap|Line Numbers
  1. int formatName(char *name[], int length)
  •  
  • You want
  • 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
  •           */
  •  
  • You haven't defined i to be anything yet, so
  • i=0;
  •           toupper(name[i]);
  • toupper() is a function call.  You need to assign the result
  • somewhere.
  • name[i] = toupper(name[i]);
  •           name[i+1]=' ';
  •  
  • This isn't going to work for many reasons.  sizeof(name)
  • doesn't give you the length of the string in name, strlen(name) does
  • that.  How do you plan to return the new string to the calling
  • function?  You need to rethink the interface.  name[length] isn't what
  • you think it is.  It only builds because your function definition is
  • wrong.
  •           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 */
  •  
  • name[length] is nonsense - that would be the last character of the
  • string, assuming length is the string length, and it would always be
  • 0.
  •  
  • for (i=0; i < strlen(name); i++)
  • or
  • for (i=0; i < length; i++)
  •           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]);
  • Same as above.  Might also want to check name[i+1] isn't '\0'.
  • if (name[i+1] != '\0')
  • name[i+1]=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. */
  •  
  • Don't understand what you're trying to do.
  • Looks like if you see a '.' or '&' or '*' you replace it with '/'.
  • If the next character is ' ' and not '\0' (silly, it can't be both)
  • replace it with '>'
  • If the prev character is ' ' and not '\0' (silly, again) replace it
  • with '>'
  •                     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]);
  • Same as above
  • name[i] = tolower(name[i]);
  •                }
  •           }
  •      }
  •  
  • You're not returning anything useful.
  •      return 0;
  • }


  • You need to have included <string.h> and <ctype.h> somewhere.

    I think all the way through you're mixed up with the
    string/pointer/array of chars thing. Read the FAQ, read a C
    programming book.

    Jim
    Nov 14 '05 #2

    This discussion thread is closed

    Replies have been disabled for this discussion.