|  | |
03-19-2006, 05:05 PM
|
#1 (permalink)
|
Monster Techie Join Date: Apr 2002 Location: California Posts: 1,423
| Modifying strings in C This question ended up on one of my exams for my CS class, however, I was wondering what the correct way is to accomplish it.
Basically I need to write a function that takes in a name string (takes in a string as an argument), say for example, in the form of "First Middle Last" and the function rearranges it in "Last First M." format. (For the sake of simplicity, let's ignore names that have more than two spaces).
Here's what I have down so far, and after the last part is where I got lost. (heck, i'm not even sure if I even need to count the number of spaces). I recall using a similar method on a lab assignment, however, I can't remember it exactly at the moment. Code: void sortName(char name[])
{
int i, count = 0;
char temp[100] = 0;
for(i = 0; i < strlen(name); i ++)
{
if(name[i] == ' ')
{
count ++;
strncpy(temp, name, i);
}
}
}
Any help would be appreciated, thanks. |
| |
03-19-2006, 10:45 PM
|
#2 (permalink)
|
Ultra Techie Join Date: Jul 2005 Posts: 530
| Here is what I would do: Code: void sortName(char name[])
{
int i;
int temppos = 0;
int position = 0;
char first[100];
char middle[100];
char last[100];
char newname[100];
char **current;
current = &first;
for (i = 0; i < strlen(name); i ++)
{
if (name[i] == ' ')
{
position++;
**current[temppos] = '\0';
temppos = 0;
if (position == 1)
{
current = &middle;
}
if (position == 2)
{
current = &last;
}
continue;
}
**current[temppos] = name[i];
temppos++;
}
**current[temppos] = '\0';
strcpy(newname, last);
strcat(newname, " ");
strcat(newname, first);
strcat(newname, " ");
strcat(newname, (char *)middle[0]);
strcat(newname, "."); // newname now contains the desired output
}
__________________ Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache) |
| |
03-21-2006, 12:17 AM
|
#3 (permalink)
|
Monster Techie Join Date: Apr 2002 Location: California Posts: 1,423
| Thanks, greatly appreciated. Hmm, never would have thought of that..... I guess that's why I'm not a computer scientist or software engineer. |
| |
03-23-2006, 06:41 AM
|
#5 (permalink)
|
Newb Techie Join Date: Jan 2006 Posts: 44
| Quote: Originally posted by TheHeadFL Here is what I would do: Code: void sortName(char name[])
{
int i;
int temppos = 0;
int position = 0;
char first[100];
char middle[100];
char last[100];
char newname[100];
char **current;
current = &first;
for (i = 0; i < strlen(name); i ++)
{
if (name[i] == ' ')
{
position++;
**current[temppos] = '\0';
temppos = 0;
if (position == 1)
{
current = &middle;
}
if (position == 2)
{
current = &last;
}
continue;
}
**current[temppos] = name[i];
temppos++;
}
**current[temppos] = '\0';
strcpy(newname, last);
strcat(newname, " ");
strcat(newname, first);
strcat(newname, " ");
strcat(newname, (char *)middle[0]);
strcat(newname, "."); // newname now contains the desired output
}
| I can't run your coding, it has many error |
| |
03-24-2006, 01:16 AM
|
#6 (permalink)
|
Ultra Techie Join Date: Jul 2005 Posts: 530
| I never tried to compile it, I just typed it up.
You should be able to resolve any errors pretty easily.
__________________ Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache) |
| |
03-24-2006, 06:46 AM
|
#7 (permalink)
|
Newb Techie Join Date: Jan 2006 Posts: 44
| But I have some confusion:
char **current;
......
current = & first;
......
**current[temppost] = '\0';
how come???
__________________ <b><font color=\"#0000FF\">Where we are today is a result of the choices we made yesterday.
Lets make the best choices today that we might have the best tomorrow.</font></b>
<font color=\"#FF0000\"><b>The road to success is always under construction.</b></font>
<b><font color=\"#0000FF\"> Everyday is a bad day, some are just worst than others.</font></b>
<font color=\"#FF0000\"><b>If you never learn to face your fears, you\'ll never learn to face life.</b></font>
<b><font color=\"#0000FF\">Live your life so that when you die, you\'re smiling and everyone around you is crying.</font></b> |
| |
03-24-2006, 12:32 PM
|
#8 (permalink)
|
Ultra Techie Join Date: Jul 2005 Posts: 530
| I dont even understand what youre asking me
__________________ Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache) |
| |
03-24-2006, 04:43 PM
|
#9 (permalink)
|
Ultra Techie Join Date: Jul 2005 Posts: 530
| I was mistaken about the amount of indirection needed. (Only one level instead of two, since its strings and strings are always indirected anyway)
This code works fine in my compiler: Code: void sortName(char name[])
{
int i;
int temppos = 0;
int position = 0;
int len = strlen(name);
char first[100];
char middle[100];
char last[100];
char newname[100];
char *current;
current = first;
for (i = 0; i < len; i ++)
{
if (name[i] == ' ')
{
position++;
current[temppos] = '\0';
temppos = 0;
if (position == 1)
{
current = middle;
}
if (position == 2)
{
current = last;
}
continue;
}
current[temppos] = name[i];
temppos++;
}
current[temppos] = '\0';
strcpy(newname, last);
strcat(newname, " ");
strcat(newname, first);
strcat(newname, " ");
middle[1] = '\0';
strcat(newname, middle);
strcat(newname, "."); // newname now contains the desired output
printf("Given Name: %s\n", name);
printf("New Name: %s\n", newname);
}
__________________ Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache) |
| |
03-25-2006, 05:28 AM
|
#10 (permalink)
|
Newb Techie Join Date: Jan 2006 Posts: 44
| Thanks, it seem better.
I have some confusion because **current is pointer of pointer or array of poiter or what else. And after that, it has the code like " current = &last " current is has ** and last is array so current = &last is imposible. But anyway, your code is now better. It looks professional (@_+)
__________________ <b><font color=\"#0000FF\">Where we are today is a result of the choices we made yesterday.
Lets make the best choices today that we might have the best tomorrow.</font></b>
<font color=\"#FF0000\"><b>The road to success is always under construction.</b></font>
<b><font color=\"#0000FF\"> Everyday is a bad day, some are just worst than others.</font></b>
<font color=\"#FF0000\"><b>If you never learn to face your fears, you\'ll never learn to face life.</b></font>
<b><font color=\"#0000FF\">Live your life so that when you die, you\'re smiling and everyone around you is crying.</font></b> |
| |  | | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | |