Solved

splitting a string in two parts

Posted on 2000-04-10
3
193 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Where is my core dump file in Ubuntu? 12 429
Eclipse Luna does not see MinGW 5 234
C dll call freezes 5 88
C hashtable library 3 71
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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now