Saptarshi Sahoo
Saptarshi Sahoo

Reputation: 97

String and String array Manipulation in c

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?

Upvotes: 2

Views: 53

Answers (2)

user6889435
user6889435

Reputation:

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;
}

Upvotes: 0

gsamaras
gsamaras

Reputation: 73366

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

Upvotes: 3

Related Questions