Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

String parsing

Posted on 1998-09-09
7
Medium Priority
?
187 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 40 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

670 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