Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

splitting a string in two parts

Posted on 2000-04-10
3
Medium Priority
?
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 300 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

618 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