473,287 Members | 3,319 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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 3230
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
    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
    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
    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
    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
    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
    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
    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
    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
    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
    by: DolphinDB | last post by:
    The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
    0
    by: DolphinDB | last post by:
    Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
    0
    by: Aftab Ahmad | last post by:
    Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
    0
    by: Aftab Ahmad | last post by:
    So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
    0
    isladogs
    by: isladogs | last post by:
    The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
    0
    by: marcoviolo | last post by:
    Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
    1
    isladogs
    by: isladogs | last post by:
    The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
    0
    by: Vimpel783 | last post by:
    Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
    1
    by: PapaRatzi | last post by:
    Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

    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.