1

I need to find the Nth word in a string which is given through standard input through redirection operators in Unix. Input is something along these lines:

But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born
5
The European languages are members of the same family.
3

Can anyone give me any idea as to how to read in the string into a char array and then get the int and use it to find the given word? I've been at it for a while and can't get it to work properly.

#define INPUT_LENGTH 400
int main(void)
{

char input[INPUT_LENGTH];
char integer[INPUT_LENGTH];
int spaces = 0;
int value;
char n;

while(fgets(input, INPUT_LENGTH, stdin)) //read in string line
  {
    while(fgets(integer, INPUT_LENGTH,stdin)) //read in int
      {
        int num = sscanf(integer, "%d", &value); //assign int val to num
           while(1 == sscanf(input, "%c", &n)) //go through string one char at a time
              if(spaces == num && !isspace(n))
                printf("%c", n); //print chars if we've reached the word
              else if(isspace(n))
                spaces++;
      }      
   }
}

I've redone most of it with the comments in mind but still can't seem to have it actually reading in the input through the operator unfortunately. I'm not certain but I don't think my fgets are correct. I'm rather new to C and am not entirely certain how they process the data even after research

  • Your question is not in scope for SO, since you are required to show some initial effort has been made to find a solution. You should read some tutorials on io and string handling in c, try it then come back with some specific problem you are having. If you have some code you have written, you should post it. – Paul Rooney Feb 16 '17 at 01:55
  • What have you tried? To avoid possible down votes and to get a good answer you may want to revise your question to be more specific as to what problem you are having. You may want to peruse… [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) … AND [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – JohnG Feb 16 '17 at 02:00
  • The empty loop body for the `while` loop is wrong — remove that semicolon. You need to read one line containing the string, and then a second line containing the number. You need two arrays to hold the lines. Then you need to [use `sscanf()` in a loop](http://stackoverflow.com/questions/3975236/how-to-use-sscanf-in-loops) to parse the words. – Jonathan Leffler Feb 16 '17 at 03:43

1 Answers1

2

Use strtok like this

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

#define INPUT_LENGTH 400

int main(void){
    char input[INPUT_LENGTH];
    char integer[INPUT_LENGTH];
    int value;

    while(fgets(input, sizeof input, stdin)) //read in string line
    {
        if(fgets(integer, sizeof integer, stdin)) //read in int
        {
            if(1==sscanf(integer, "%d", &value)) //assign int value to value
            {
                char *word = strtok(input, " \t\n");
                int n;
                for(n = 1; word != NULL && n < value; ++n){// 1 origin
                    word = strtok(NULL, " \t\n");
                }
                if(word != NULL && n == value)
                    puts(word);//Nth word
                else
                    puts("No word");
            }
            else {
                printf("Numerical value is not specified.\n");
            }
        }
        else {
            printf("There is no numeric specification line.\n");
        }
    }
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • Oh sorry! I should've mentioned part of the task was to complete it without tokenizing the string, also when I try running it with the command line using ./NthWord – NicholasNickNic Feb 17 '17 at 02:49
  • @NicholasNickNic What is the content of `nthword.txt`? It seems to work. [DEMO](http://ideone.com/05r2LJ) – BLUEPIXY Feb 17 '17 at 03:11