Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function that standardize a string with lowercase and uppercase ?

Status
Not open for further replies.

Exode

Programmer
Feb 17, 2004
14
CA
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

Code:
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;
}
 
It seems there are too many weakness in the implementation of these specifications (to capitalize a name):
1. The 1st parameter must be:
Code:
char* name; /* Pointer to char (array) */
/*not*/char* name[] /* denotes array of pointers to char */
So you can't compile your snippet without errors! No matter is your switch OK or not...
2. toupper(c) from <type.h> or <stdlib.h> returns uppercase variant of its (char) argument - it not modifies any memory location. So you must write
Code:
name[i] = tolower(name[i]); /* and so on in this manner*/
3. I think you can't realloc(ate) name pointer because of your routine can't know if it's dynamically allocated pointer or not (is automatic or static storage array). So you have no rights to change name argument size or location in your routine.
4. You wrote (in comments):
If we find a space, then the next character
is a new word and starts with an uppercase.

It's wrong statement. How about two (or more) spaces between 1st and last names? Why it's impossible case?
5. You can't insert char between C array elements. You can rewrite its elements only. So you can't insert spaces in place. You need move next array elements to obtain new char slot and to save unprocessed contents.

It seems at first we need to clarify our converter specifications. May we suppose that size of name arg so large that we can expand (move to right end) its contents? Or we must define new (target) array (pointer) parameter for convertion results? How about C-string null terminator byte (is it C-string or not). If it's, why length parameter passed?

Then we must invent some kind of scanner alg (lexical analyzer) to select words from arg string (no matter how many spaces between words etc). Of course, we may use strtok() or another library routines (isalpha(), for example).

Well, I can write the proper capitalizer, but it's more better for you to do it now...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top