Solved

String parsing

Posted on 1998-09-09
7
181 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now