1

this might be some basic question, but I really can't find an answer: I need to split a string that has two numbers, into two strings in C.

Example:

1->2 into 1 and 2

I'm using strtok, but it takes a series of chars to split, you can't specify a sequence to be the divisor.

I can't use anything outside from ANSI C.

Any help will be appreciated!

How can you flag this as exact duplicate? It's not even close...

Lautaro
  • 93
  • 2
  • 9
  • see http://stackoverflow.com/questions/2523467/how-to-split-a-string-to-2-strings-in-c – ericosg Apr 22 '13 at 05:05
  • @Lautaro Hey,don't mind if I sounded rough.I am telling from my own past experience as a newbie.If you read thoroughly about something,then you'll be surprised how many nuanced discoveries you'll make.Instant answers may seem tempting to all of us, but it always pays in the long run if we toil a bit. – Rüppell's Vulture Apr 22 '13 at 05:10
  • 1
    @ericosg That link isn't relevant since `strtok` only handles single-character delimiters and that's all that's shown at the link. It can be done with `strstr`. – Jim Balter Apr 22 '13 at 05:28
  • @ericosg with all due respect, your answers don't help too much... – Lautaro Apr 22 '13 at 07:03
  • @Sheer Fish don't mind, I didn't take it the wrong way – Lautaro Apr 22 '13 at 07:05

3 Answers3

5

Use 'sscanf()'. Don't ask someone to do it for you as it's too easy if you give a few minutes to understand it, especially its format specifier string nuances.Here's a link that would suffice :

http://www.cplusplus.com/reference/cstdio/sscanf/

Hint : sscanf (source_string,"%d->%d",&num1,&num2);

Rüppell's Vulture
  • 3,583
  • 7
  • 35
  • 49
  • I don't think this is what the OP was looking for and also your answer does not seem that helpful. – ericosg Apr 22 '13 at 05:06
  • @ericosg Let's hope the edit helps.Else I'll delete my answer. – Rüppell's Vulture Apr 22 '13 at 05:08
  • @ericosg I seem to have forgotten that giving just links just don't count as good answers in this forum,as links could get broken and all said and done, one has to look again in that new site for the answer. – Rüppell's Vulture Apr 22 '13 at 05:16
  • Thanks! This actually helped. I'm in a bit of a hurry, pretty tired and it's really late where I live, I should have come around this by myself. – Lautaro Apr 22 '13 at 07:06
2

Well here's something to get you started:

const char *FindSubString( const char *sub, const char *src )
{
  // Loop through the source string
  while(*src++)
  {
    // Only check for matching character in beginning of sub sequence
    if(*src == *sub)
    {
      return src;
    }
  }
}

This just finds a pointer to the first matching character in a sub-sequence and a source string. I'm sure you can get together something in the inner-loop that checks to see if the sub string matches the next few characters in the src string.

RandyGaul
  • 1,915
  • 14
  • 21
1

This is a brute force method. just suitable for a quick fix.

#include <stdlib.h>
#include <ctype.h>

void extract_2_numbers (char *str, int *a, int *b) {
  *a = atoi (str);
  while (isdigit(*str)) str++;
  while (!isdigit(*str)) str++;
  *b = atoi (str);
}

int main () {
  int x, y;
  char *string = "12--->24";

  extract_2_numbers (string, &x, &y);
  printf ("%d\t%d\n", x, y);
  return 0;
}

Prints this output :

12    24
Sriharsha
  • 51
  • 4