You are here:

C/split string

Advertisement


Question
sir i want to split this string 200.0.0.90-200.0.0.223 by hiphen (-)
and store it in  two seperate variables.pls give me sample code as
soon as possible

Answer
You can use strtok() to do that.
Also, you can write your own code.

Sample code:

split(char *original, char * split1, char * split2)
{
  int i = 0;
  int j = 0;
  int flag = 0;

  while (original[i] != '\0')
  {
     if (flag == 0)
     {
        if (original[i] == '-')
        {
           flag = 1;
           split1[i] = '\0';
        }
        else
        {
           split1[i] = original[i];
        }
     }
     else
     {
        split[j++] = original[i];
     }
     i++;
}

-Narendra

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.