Solved

splitting a string in two parts

Posted on 2000-04-10
3
223 Views
Last Modified: 2010-04-15
Hi, all:
  I have a string with spaces. I'd like to split the string in two parts, in the first part I want one of the word of the string, let's say the third, and in the other part I want the rest.
  I mean:

String= "first two three fourth fifth"
String_1= "three"
String_2= "first two fourth fifth"

In principle I don't know the number of words in the string, I only know the position of the word I want.

I know it should be easy, but I couldn't do it without problems with pointers and other strange things.

What's is the standard way to do it?. Some code would be fantastic.

Thanks and regards.
0
Comment
Question by:xilgaro
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 2701508
Basically, you want to remove 'Three' from the above string. The following code will do that:

char* RemoveFromString ( char* pszString, char*, pszWord, char* pszOutBuffer)
{
  char* pc;

  pc = strtok ( pszString, " ");

  while ( pc)
  {
    if  (   !strcmp (   pc, pszWord))
            continue;

    strcat  (   pszOutBuffer,   pc);
    strcat  (   pszOutBuffer,   " ");

    pc  =   strtok  (   NULL,   " ");
  }

  return    (   pszOutBuffer);
}

It copies each word from 'pszString' (->String) to 'pszOutBuffer' (->String_2), but omits 'pszWord' (->String_1).

Make sure that pszOutBuffer is large enough ;-)

Feel free to ask if you need more information!
0
 
LVL 86

Expert Comment

by:jkr
ID: 2702507
Sorry, minor corrction:

Change

    strcat  (   pszOutBuffer,   pc);
    strcat  (   pszOutBuffer,   " ");

    pc  =   strtok  (   NULL,   " ");

to read

    strcat  (   pszOutBuffer,   pc);

    pc  =   strtok  (   NULL,   " ");

   if ( pc)
    strcat  (   pszOutBuffer,   " ");
0
 
LVL 9

Expert Comment

by:Pacman
ID: 2703343
beginning from the end of "String" look for a blank.
Then you get the position of the last word.
Now you can extract the two substrings using strcpy() and strncpy()

Try this code. Maybe you must modify it a little bit, I've written it "from brain to keyboard" and didn't test it.
But it should show the idea.

void Split (char *string, char *part1, char *part2)
{
   int  i;
   int len = strlen(string);

   for (i=len-1; i>=0 && i!=' '; i--)
      ;

   if (i >= 0)   // found a blank
   {
      strncpy(part1, string, i);
      part1[i] = 0;

      strncpy(part2, &string[i+1], len-i);
      part2[len-i] = 0;
   }
   else
   {
      part1[0] = 0;
      part2[0] = 0;
   }
}


}
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stack 22 172
Linux context switch  - loop takes long time to process 6 129
Problem to ASCII 1 197
c++ getting the first 10 characters of a char* string 11 99
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 opening and writing to files 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.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question