Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 03-19-2006, 05:05 PM   #1 (permalink)
Martin's Avatar
 
Monster Techie

Join Date: Apr 2002

Location: California

Posts: 1,423

Martin is on a distinguished road

Default 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.
__________________
Martin's Page of Stuff

I judge you when you use poor grammar.
Martin is offline  
Old 03-19-2006, 10:45 PM   #2 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

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)
TheHeadFL is offline  
Old 03-21-2006, 12:17 AM   #3 (permalink)
Martin's Avatar
 
Monster Techie

Join Date: Apr 2002

Location: California

Posts: 1,423

Martin is on a distinguished road

Default

Thanks, greatly appreciated. Hmm, never would have thought of that..... I guess that's why I'm not a computer scientist or software engineer.
__________________
Martin's Page of Stuff

I judge you when you use poor grammar.
Martin is offline  
Old 03-23-2006, 12:21 AM   #4 (permalink)
Chankama's Avatar
 
Monster Techie

Join Date: Jan 2005

Location: Canada

Posts: 1,522

Chankama will become famous soon enough

Default

Well without going through all the code, and writing my own code, I would imaging the following standard functions would be useful ..

strtok
strcat

I would imagine, you can write the program in about 12 lines. Extract the string tokens and then rearrange them.

http://www.cplusplus.com/ref/cstring/strtok.html
http://www.cplusplus.com/ref/cstring/strcat.html
Chankama is offline  
Old 03-23-2006, 06:41 AM   #5 (permalink)
 
Newb Techie

Join Date: Jan 2006

Posts: 44

time_come_back

Default

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
time_come_back is offline  
Old 03-24-2006, 01:16 AM   #6 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

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)
TheHeadFL is offline  
Old 03-24-2006, 06:46 AM   #7 (permalink)
 
Newb Techie

Join Date: Jan 2006

Posts: 44

time_come_back

Default

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>
time_come_back is offline  
Old 03-24-2006, 12:32 PM   #8 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

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)
TheHeadFL is offline  
Old 03-24-2006, 04:43 PM   #9 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

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)
TheHeadFL is offline  
Old 03-25-2006, 05:28 AM   #10 (permalink)
 
Newb Techie

Join Date: Jan 2006

Posts: 44

time_come_back

Default

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>
time_come_back is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On