473,498 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function that standardize a string with lowercase and uppercase ?

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
1 3248
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 thread has been closed and replies have been disabled. Please start a new discussion.

    Similar topics

    9
    4933
    by: Penn Markham | last post by:
    Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
    13
    6327
    by: Nige | last post by:
    To save me re-inventing the wheel, does anyone have a function to capitalise each word in form data? I was thinking along the lines of capitalise the first letter of the string, then any other...
    9
    2986
    by: drhowarddrfinedrhoward | last post by:
    I see a number of pages with functions like MM_somefunction(). Where does the MM_ come from? I don't see it in any books I'm studying.
    30
    3258
    by: Steve Edwards | last post by:
    Hi, I'm re-writing some code that had relied on some platform/third-party dependent utility functions, as I want to make it more portable. Is there a standard C/C++/stl routine for changing an stl...
    6
    8615
    by: bruce | last post by:
    hi... i'm running into a problem where i'm seeing non-ascii chars in the parsing i'm doing. in looking through various docs, i can't find functions to remove/restrict strings to valid ascii...
    2
    10281
    by: evantri | last post by:
    Hi everyone, I am required to write a standard C function to import the single character variable and return the lowercase version of the character int upper_to_lower ( char singlecharacter ) {...
    4
    4768
    by: jerger | last post by:
    i have a great program now with the help of a member from this site, but i need a little customization to meet the needs of non-english speakers... who might accidendtly type punctuation which would...
    4
    2041
    by: cpptutor2000 | last post by:
    Could some C guru provide some hints on my problem? I am trying to sort an array of character strings, where each string contains lowercase, uppercase, digits as well as non-alphanumeric characters...
    2
    2068
    by: Bob Nelson | last post by:
    Concerning program startup in a hosted environment, both C90 and C99 require that the implementation provide main's ``argv'' strings in lowercase if the host environment is not capable of supply...
    0
    7126
    marktang
    by: marktang | last post by:
    ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
    0
    7005
    by: Hystou | last post by:
    Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
    0
    7168
    Oralloy
    by: Oralloy | last post by:
    Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
    0
    7210
    jinu1996
    by: jinu1996 | last post by:
    In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
    1
    6891
    by: Hystou | last post by:
    Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
    0
    7381
    tracyyun
    by: tracyyun | last post by:
    Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
    0
    3096
    by: TSSRALBI | last post by:
    Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
    1
    659
    muto222
    by: muto222 | last post by:
    How can i add a mobile payment intergratation into php mysql website.
    0
    293
    bsmnconsultancy
    by: bsmnconsultancy | last post by:
    In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

    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.