Solved

String parsing

Posted on 1998-09-09
7
185 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
[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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 recursion 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

739 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