Solved

String parsing

Posted on 1998-09-09
7
184 Views
Last Modified: 2012-05-04
I have a pointer string as defined below:
   char *mystr={"This \n is my\n string." }

I want to make this string three distinct strings such as "This "," is my"," string" ( I want to count and delete \n's. I want to put these strings in a string array as myarray[0]="This ", myarray[1]=" is my",
... What shoul i do?
0
Comment
Question by:Cumhur
7 Comments
 
LVL 5

Expert Comment

by:ecw
ID: 1252559
#include <string.h>
int split_string(char *str, char **myarray, int array_len)
{
char *c;
int count;
  for (count = 0; str && count < array_len; ++count, str = c) {
    myarray[count] = str;
    if ((c = strchr(str, '\n')))  *c++ = '\0';
  }
  return count;
}

0
 
LVL 2

Accepted Solution

by:
JYoungman earned 20 total points
ID: 1252560
sscanf(mystr, "%[^\n]\n%[^\n]\n%[^\n]",
      &myarray[0], &myarray[1], &myarray[2]);

Note that this copies the three strings.  If you just wanted pointers to those parts of the srting you could use something like strtok().

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1252561
Hello all....What does the ** means that is in front of myarray...

e.g.  int split_string(char *str, char **myarray, int array_len)

Thanks

//Viktor
0
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
LVL 84

Expert Comment

by:ozo
ID: 1252562
pointer to pointer to char
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1252563
And how can it be used??? Would you please give me a simple example?? ThanQ
0
 
LVL 5

Expert Comment

by:ecw
ID: 1252564

char *myarray[10];
int count = split_string(strdup("one\ntwo\nthree\nfour\n"), myarray, 10)
int i;
  printf("Split string into %-d bits\n", count);
  for (i = 0; i < count; ++i) {
   printf("line %-d is %s\n", i+1, myarray[i]);
  }

0
 

Expert Comment

by:sanderson1
ID: 1252565
/* This will give you an array of pointers to each of your strings Output: This | is my| string.| */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXSTRINGS 10
int main(int argc, char *argv[])
{
  char *mystr={"This \n is my\n string."};
  char *strings[MAXSTRINGS];
  char *p;
  int i=0;
  int count=0;

  p = strtok(mystr, "\n");
  strings[i] = p;

  while((p = strtok('\0', "\n"))!=NULL)  {
     i++;
     strings[i] = p; }

  /* output strings */
  for(count = 0; count <= i; count++)
    printf("%s|", strings[count]);

  /* return number of \n's */
  return i;
}
0

Featured Post

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
port mapped I/o query 10 207
List out all word 7 320
How to creat good software interface ? 1 69
How to install SVN Command Line Client? 5 81
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 pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

821 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