2

I want to create a function to split a text using a separator in C. Two parameters text and separator will be passed to the function and the function should return an array of chars.

For example if the string is Hello Word of C and the separator is a white space.

Then the function should return,

 0. Hello 
 1. Word  
 2. of 
 3. C

as an array of chars.

Any suggestions?

10
  • 4
    Step 1: Write a function that takes 2 parameters, text, and a separator, with a return value of an array of chars. (clue: We won't write your code for you. Write some code, post it, and we'll help you modify it) Commented Nov 30, 2010 at 22:09
  • 2
    you mean "and return an array of chararrays" ? Commented Nov 30, 2010 at 22:19
  • looks similar to stackoverflow.com/questions/4291468/… Commented Dec 1, 2010 at 10:08
  • @abelenky: as @user411313 pointed, returning an array of char is not an option. It should be an array of char arrays. Also providing a buffer (and size) where to store results would probably be a good idea to avoid complex memory allocation. Commented Dec 1, 2010 at 10:12
  • Looking at your use profile, pho3nix, you should (a) be able to ask a much better question, and (b) know better than to ask this. Commented Dec 1, 2010 at 14:59

5 Answers 5

4

Does strtok not suit your needs ?

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

2 Comments

strtok is usually not an acceptable answer to homework assignments.
@abelenky: This isn't tagged homework, and strtok is standard C. It's also arguably the screwiest function in the C library, but it would work here.
1

As someone else already said: do not expect us to write your homework code, but here's a hint: (if you're allowed to modify the input string) Think about what happens here:

char *str = "Hello Word of C"; // Shouldn't that have been "World of C"???
str[5] = 0;
printf(str);

Comments

1

Well, same solution as abelenky, but without the useless crap and obfuscation of test code (when something - like printf - should be written twice, I do not introduce a dummy boolean to avoid it, didn't have I read something like that somewhere ?)

#include<stdio.h>

char* SplitString(char* str, char sep)
{
    return str;
}

main()
{
    char* input = "Hello Word of C";
    char *output, *temp;
    char * field;
    char sep = ' ';
    int cnt = 1;
    output = SplitString(input, sep);

    field = output;
    for(temp = field; *temp; ++temp){ 
       if (*temp == sep){
          printf("%d.) %.*s\n", cnt++, temp-field, field);
          field = temp+1;
       }
    }
    printf("%d.) %.*s\n", cnt++, temp-field, field);
}

Tested with gcc under Linux:

1.) Hello
2.) Word
3.) of
4.) C

Comments

0

My solution (addressing comments by @kriss)

char* SplitString(char* str, char sep)
{
    char* ret = str;
    for(ret = str; *str != '\0'; ++str)
    {
        if (*str == sep)
        {
            *str = '\001';
        }
    }
    return ret;
}

void TestSplit(void)
{
    char* input = _strdup("Hello Word of C");
    char *output, *temp;
    bool done = false;

    output = SplitString(input, ' ');

    int cnt = 1;
    for( ; *output != '\0' && !done; )
    {
        for(temp = output; *temp > '\001'; ++temp) ; 
        if (*temp == '\000') done=true;
        *temp = '\000';
        printf("%d.) %s\n", cnt++, output);
        output = ++temp;
    }
}

Tested under Visual Studio 2008

Output:

1.) Hello
2.) Word
3.) of
4.) C

5 Comments

is it a Joke ? The Job is fully done by TestSplit. I had some fun anyway writing my own answer on the same model...
SplitString finds the separators, tags them with \001, (leaving the end of the string tagged with \000), and returns the answer. Its just up to the output function to iterate through each string and display it. :) Sorry if you don't like the answer, but I'm convinced it meets the spec.
the point is not I do not like it, but that it does nothing usefull. You have the exact same amount of work to do after calling the function to parse the results that you would have to extract the data from the initial string without calling the function. I agree it could have been worse, it could be more difficult to extract the data from results than before calling the function (that can even be useful in some cases, like compressor/decompressor). OK, you meet the spec, but your function does nothing useful. I believe it's good practice that such functions doesn't exists.
If you used '\0' as your "tag", you could use regular strlen and family to manipulate the strings, so long as you keep track of where the original end of the string was.
@Gabe: yes, that's a good point, but you still have to add some information to do that (keep the end), even if that is indeed not much.
0

I would recomend strsep. It's easier to understand than strtok, yet it dissects existing string, making it sequence of tokens. It's up to you to decide, if it needs to be copied first or not.

Comments

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.