1

Implement char * ithword(char * str, int i) that will return the ith word in str. The words are alphanumeric and separated by non-alphanumeric characters. The function will allocate memory for the word returned, and return a pointer to it.

My approach is : Start traversing the string and count the numbers of spaces and non-alphanumeric characters and when the count reaches i-1, start printing till you get another space or non-alphanumeric characters.

Is it correct?

max
  • 167
  • 2
  • 9
  • 2
    `Is it correct?` Every journey starts with a single step. – 101010 Nov 04 '15 at 08:10
  • Is the `C++` tag a mistake? The question looks like it is intended to be (bad, but my opinion) `C`, and the answers will be very different in the 2 different languages. [`isalnum`](http://en.cppreference.com/w/c/string/byte/isalnum) is likely to be your friend (more so in the `C` case than the `C++`, I think). – BoBTFish Nov 04 '15 at 08:10
  • 4
    Sounds correct. If there are subtle bugs, you'll get it after coding, so i would suggest you start coding. – Haris Nov 04 '15 at 08:13
  • 1
    The best approach is keeping simple which is what your solution is. Try coding it and cover the edge cases and you would be good to go. For CPP, inbuilt functions can be used which will do a similar thing. – thepace Nov 04 '15 at 11:32
  • What about consecutive whitespaces? Is that something you have to handle? – Karoly Horvath Nov 04 '15 at 11:35

0 Answers0