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;
-
}
-
1 3230
On Tue, 26 Oct 2004 02:03:23 -0400, "The_Kingpin"
<th**********@hotmail.com> wrote: 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)
-
-
You want
-
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
- */
-
-
You haven't defined i to be anything yet, so
-
i=0;
-
- toupper(name[i]);
-
toupper() is a function call. You need to assign the result
-
somewhere.
-
name[i] = toupper(name[i]);
-
- name[i+1]=' ';
-
-
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.
-
- 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 */
-
-
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++)
-
- 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]);
-
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]);
- break;
- /* If the character is not a letter, we swap it
- for a / and we check if there is spaces between
- it. */
-
-
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 '>'
-
- 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]);
-
Same as above
-
name[i] = tolower(name[i]);
- }
- }
- }
-
-
You're not returning anything useful.
-
- 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Penn Markham |
last post by:
Hello all,
I am writing a script where I need to use the system() function to call
htpasswd. I can do this just fine on the command line...works great
(see attached file, test.php). When my...
|
by: Nige |
last post by:
To save me re-inventing the wheel, does anyone have a function to
capitalise each word in form data? I was thinking along the lines of
capitalise the first letter of the string, then any other...
|
by: drhowarddrfinedrhoward |
last post by:
I see a number of pages with functions like MM_somefunction(). Where does
the MM_ come from? I don't see it in any books I'm studying.
|
by: Steve Edwards |
last post by:
Hi,
I'm re-writing some code that had relied on some platform/third-party
dependent utility functions, as I want to make it more portable.
Is there a standard C/C++/stl routine for changing an stl...
|
by: bruce |
last post by:
hi...
i'm running into a problem where i'm seeing non-ascii chars in the parsing
i'm doing. in looking through various docs, i can't find functions to
remove/restrict strings to valid ascii...
|
by: evantri |
last post by:
Hi everyone, I am required to write a standard C function to import the single character variable and return the lowercase version of the character
int upper_to_lower ( char singlecharacter )
{...
|
by: jerger |
last post by:
i have a great program now with the help of a member from this site, but i need a little customization to meet the needs of non-english speakers... who might accidendtly type punctuation which would...
|
by: cpptutor2000 |
last post by:
Could some C guru provide some hints on my problem? I am trying to
sort an array of character strings, where each string contains
lowercase, uppercase, digits as well as non-alphanumeric characters...
|
by: Bob Nelson |
last post by:
Concerning program startup in a hosted environment, both C90 and C99 require
that the implementation provide main's ``argv'' strings in lowercase if the
host environment is not capable of supply...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |