Function that standardize a string with lowercase and uppercase ?

T

The_Kingpin

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;
}
 
J

Jim

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)[/QUOTE]

You want
int formatName(char *name, int length)
[QUOTE]
{
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
*/[/QUOTE]

You haven't defined i to be anything yet, so
	i=0;
[QUOTE]
toupper(name[i]);[/QUOTE]
toupper() is a function call.  You need to assign the result
somewhere.
	name[i] = toupper(name[i]);
[QUOTE]
name[i+1]=' ';[/QUOTE]

	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.
[QUOTE]
for(i;i<length;i++)
{
realloc(name[length], sizeof(name[length]+1));
name[i+1]=name[i];
}[/QUOTE]
[QUOTE]
}
else
{
/* The word has more than 1 letter */[/QUOTE]

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++)
[QUOTE]
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]);[/QUOTE]
	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]);[QUOTE]
break;

/* If the character is not a letter, we swap it
for a / and we check if there is spaces between
it. */[/QUOTE]

	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 '>'
[QUOTE]
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]);[/QUOTE]
	Same as above
			name[i] = tolower(name[i]);[QUOTE]
}
}
}[/QUOTE]

	You're not returning anything useful.
[QUOTE]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top