• C

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

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_Kingpin08Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

van_dyCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ssnkumarCommented:
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
ssnkumarCommented:
Hi van_dy,

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

-ssnkumar
0
Become an IT Security Management Expert

In today’s fast-paced, digitally transformed world of business, the need to protect network data and ensure cloud privacy has never been greater. With a B.S. in Network Operations and Security, you can get the credentials it takes to become an IT security management expert.

van_dyCommented:
haha, that has happened quite a few times with me too
0
The_Kingpin08Author Commented:
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
van_dyCommented:
>> 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
The_Kingpin08Author Commented:
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
van_dyCommented:
well , make that  realloc(name, strlen(name) + 1);    this is because sizeof(name) willl return 4, sizeof a char *.
0
van_dyCommented:
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
van_dyCommented:
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
ssnkumarCommented:
>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
The_Kingpin08Author Commented:
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
van_dyCommented:
isalpha()  from ctype.h   i think i said about it in my first post
0
The_Kingpin08Author Commented:
yeah sorry, I'm on 2 boards plus my project so I get mixed up a little.

Thanks again
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.