HealthLinks is your destination for reliable, understandable, and credible health information and expert advice that always keeps why you came to us in mind.

How to Split String in C

104 20
    • 1). Open a text editor.

    • 2). Paste the following code:

      #include <iostream>

      using namespace std;

      void split_string(char *orig, char *first, char *second, int pos) {

      char newline = '\n';

      int x;

      int counter = pos;

      for (x = 0; 1; x++) {

      if (orig[counter] == newline) break;

      second[x] = orig[counter];

      counter++;

      }

      second[x] = newline;

      for (x = 0; x < pos; x++) {

      first[x] = orig[x];

      }

      first[pos] = newline;

      }

      int main() {

      char *s = "An array of characters.\n";

      char *t = (char *) calloc(5, sizeof s);

      char *u = (char *) calloc((sizeof t)-5, sizeof s);

      split_string(s,t,u,5);

      cout << "first string " << t;

      cout << "second string " << u;

      return 0;

      }

    • 3). Save your work and compile it.

Source...

Leave A Reply

Your email address will not be published.