Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

Function that standardize a string with lowercase and uppercase ?

Posted on 2004-10-25
Medium Priority
241 Views
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

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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 7
• 4
• 3

LVL 5

Accepted Solution

van_dy earned 600 total points
ID: 12407966
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

ssnkumar earned 400 total points
ID: 12407991
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

ID: 12407998
Hi van_dy,

I think I have posted at the same time you were posting!

-ssnkumar
0

LVL 5

Expert Comment

ID: 12408007
haha, that has happened quite a few times with me too
0

Author Comment

ID: 12408063
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

ID: 12408073
>> 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

ID: 12408108
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

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

LVL 5

Expert Comment

ID: 12408128
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

ID: 12408182
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

ID: 12408189
>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

ID: 12408309
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

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

Author Comment

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

Thanks again
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
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â€¦
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 opening and reading files in the C programming language.
Suggested Courses
Course of the Month8 days, 8 hours left to enroll