2

I'm trying to write a string spliter function in C.It uses space as delimiter to split a given string in two or more. It more like the split funtion in Python.Here is the code:-

#include <stdio.h>
#include <string.h>


void slice_input (char *t,char **out)
{
    char *x,temp[10];
    int i,j;
    x = t;
    j=0;
    i=0;
    for (;*x!='\0';x++){
        if (*x!=' '){
            temp[i] = *x;
            i++;
        }else if(*x==' '){
            out[j] = temp;
            j++;i=0;
        }
    }
}

int main()
{
    char *out[2];
    char inp[] = "HEllo World ";

    slice_input(inp,out);
    printf("%s\n%s",out[0],out[1]);
    //printf("%d",strlen(out[1]));
    return 0;
}

Expeted Output:-

HEllo
World

but it is showing :-

World
World

Can you help please?

1
  • 2
    May the debugger be your friend Commented Oct 21, 2017 at 7:21

2 Answers 2

3

out[j] = temp;

where temp is a local variable. It will go out of scope as soon as your function terminates, thus out[j] will point to garbage, invoking Undefined Behavior when being accessed.

A simple fix would be to use a 2D array for out, and use strcpy() to copy the temp string to out[j], like this:

#include <stdio.h>
#include <string.h>

void slice_input(char *t, char out[2][10]) {
  char *x, temp[10];
  int i,j;
  x = t;
  j=0;
  i=0;
  for (;*x!='\0';x++) {
    if (*x!=' ') {
      temp[i] = *x;
      i++;
    } else if(*x==' ') {
      strcpy(out[j], temp);
      j++;
      i=0;
    }
  }
 }


int main()
{
  char out[2][10];
  char inp[] = "HEllo World ";

  slice_input(inp,out);
  printf("%s\n%s",out[0],out[1]);
  return 0;
}

Output:

HEllo
World
Sign up to request clarification or add additional context in comments.

8 Comments

That's right but I think it would be better for OP to use strtok() soluion
@KrzysztofSzewczyk the OP wants to implement his own strtok(), probably for practice.
But how this would be practice then @KrzysztofSzewczyk? =)
Good point @KrzysztofSzewczyk, but you need to practice too. :)
I don't know, seems the leading way to learn C has become mindlessly staring at the screen wondering why things don't work, guess and what to change, recompile and hope, ... then repeat.
|
0

http://www.cplusplus.com/reference/clibrary/cstring/strtok/

From the website:

char * strtok ( char * str, const char * delimiters ); On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of last token as the new starting location for scanning.

Once the terminating null character of str is found in a call to strtok, all subsequent calls to this function (with a null pointer as the first argument) return a null pointer.

Parameters

str C string to truncate. Notice that this string is modified by being broken into smaller strings (tokens). Alternativelly [sic], a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended. delimiters C string containing the delimiter characters. These may vary from one call to another. Return Value

A pointer to the last token found in string. A null pointer is returned if there are no tokens left to retrieve.

Example

/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}

You can use this function to split string into tokens - there is no need to use some own functions. Your code looks like garbage, please format it. Your source propably would look like this:

char *
strtok(s, delim)
    char *s;            /* string to search for tokens */
    const char *delim;  /* delimiting characters */
{
    static char *lasts;
    register int ch;

    if (s == 0)
    s = lasts;
    do {
    if ((ch = *s++) == '\0')
        return 0;
    } while (strchr(delim, ch));
    --s;
    lasts = s + strcspn(s, delim);
    if (*lasts != 0)
    *lasts++ = 0;
    return s;
}

2 Comments

Actually i want to make a funtion like spilt of python, where i mention the delimiter amd it would return an array of tokens,WITHOUT USING LIBRARY FUNCTIONS.Btw thanks for your help
yeah im trying to comprehend that only...thanks for that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.