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