Solved

Function that standardize a string with lowercase and uppercase ?

Posted on 2004-10-25
234 Views
Last Modified: 2010-04-15
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;
}
0
Question by:The_Kingpin08
    14 Comments
     
    LVL 5

    Accepted Solution

    by:
    int formatName(char *name[], int length)   <---------- you should pass char *name, not char *name[]
    {
     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]); <--------- the value for i will contain junk, you should initialize it to zero
              name[i+1]='. '; <-------- supposed to introduce a .


                     case '.': case '&': case '*':        <----------- not the most general way, use isalpha() from ctype.
                             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;


    actually this cse test should be place before making any changes to the string, like u do here by calling toupper at the very start.
    0
     
    LVL 8

    Assisted Solution

    by:ssnkumar
    I think the mistake that you have done is with the parameter of the function:
    change:
    >char *name[]
    to:
    char *name

    name is just a array of characters, not a array of strings.

    Also tell me what is the exact problem you are facing.
    If you could give me the input, expected output and the output you are getting, it will help me analyzing the problem.

    -ssnkumar
    0
     
    LVL 8

    Expert Comment

    by:ssnkumar
    Hi van_dy,

    I think I have posted at the same time you were posting!
    Sorry for the duplicate answer.....

    -ssnkumar
    0
     
    LVL 5

    Expert Comment

    by:van_dy
    haha, that has happened quite a few times with me too
    0
     

    Author Comment

    by:The_Kingpin08
    Ok thanks guys, this really helps me.

    By dropping the [] of my parameter, how do I change my realloc function. Would "realloc(name, sizeof(name + 1));" be ok ?

    BTW is there a built-in function that automatically return me the length of a string ? I know there's one in VB, strlength I think or something like that...

    Amd ssnkumar, I have been able to test it so far but it won't be long, just wanted some feedback on the function before testing it :)

    Thanks, Frank
    0
     
    LVL 5

    Expert Comment

    by:van_dy
    >> here a built-in function that automatically return me the length of a string ?
    yes strlen(a) returns the length of the string pointed to by a.

    Would "realloc(name, sizeof(name + 1));" be ok ? make it realloc(name, sizeof(name) + 1); then it will be ok
    0
     

    Author Comment

    by:The_Kingpin08
    Ok thanks again van !

    one last question, let's say I have a defined struct:

     struct book
     {
         char title[80];
         char autor[40];
         char editor[20];
         char ISBN[10];
         char subject[20];
         char releaseDate[10];
     };
     typedef struct book bookInfo;

    Each time I read 6 lines, I take them to form a new struct "bookInfo BOOK;"

    How could I use the record "title" of my struct BOOK and pass it in parameter to my function ? Would I be able to use lowercase and uppercase function on it ?
    You might not have enough information to understand correctly, so if you need more code let me know and I'll post my 2 other functions.

    Thanks a lot ! Frank
    0
     
    LVL 5

    Expert Comment

    by:van_dy
    well , make that  realloc(name, strlen(name) + 1);    this is because sizeof(name) willl return 4, sizeof a char *.
    0
     
    LVL 5

    Expert Comment

    by:van_dy
    as an example

    bookinfo   mybook;
    .............// set the various elements of mybook
    somefunction(mybook.title);


    ....

    int somefunction(char *title)
    {
    ..............// yes u can use toupper and tolower here like title[someindex] = toupper(title[someindex]);
    0
     
    LVL 5

    Expert Comment

    by:van_dy
    To make it clearer for you;

    #include <stdio.h>
    #include <stdlib.h>

    void func(char *a)
    {
          a[3] = toupper(a[3]);
    }

    int main()
    {
          char *a = malloc(10);
          strcpy(a, "tommy");
          func(a);
          printf("%s \n", a);
          return 0;
    }

    the above simple will produce the output "tomMy"
    0
     
    LVL 8

    Expert Comment

    by:ssnkumar
    >How could I use the record "title" of my struct BOOK and pass it in parameter to my function ?
    You can send the strcture BOOK itself to the function and use "." operator to use the member title. Like this:
    struct books
    {
        char title[128];
        char author[128];
    };

    processBooks(struct books mybook)
    {
         printf("Title    = %s\n", mybook.title);
         printf("Author = %s\n", mybook.author);
    }

    main()
    {
        struct books BOOK;

        processBooks(BOOK);
    }

    >Would I be able to use lowercase and uppercase function on it ?
    What do you mean by this? You want to change the case of title/author? Yes, you can d it.

    -ssnkumar
    0
     

    Author Comment

    by:The_Kingpin08
    thanks, it's working fine. Just need a couple of things done and I'll post the final result.

    BTW is there a function that verify if the character is a letter ?

    Thanks. Frank
    0
     
    LVL 5

    Expert Comment

    by:van_dy
    isalpha()  from ctype.h   i think i said about it in my first post
    0
     

    Author Comment

    by:The_Kingpin08
    yeah sorry, I'm on 2 boards plus my project so I get mixed up a little.

    Thanks again
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    port mapped I/o query 10 99
    Details to do the search 56 129
    Passing command line arguments in C 10 64
    C Language combined operators 28 86
    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now