Solved

String parsing

Posted on 1998-09-09
7
183 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

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…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

777 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