Hello everyone
Im working on a function that takes a character array and returns the nth word in the array into another array using pointers. But Soooomehow, its not working... sigh...
It would be very much appreciated if someone can tell me what im doing wrong
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *sbreak(const char *st, char *rst, int y)
{
int x = 0;
int rli = 0;
int n = strlen(st);
for(int i = 0; i < n; i++)
{
printf("%c",st[i]);
}
for(int i = 0; i < n; i++)
{
if(st[i] == " ")
{
x++;
continue;
}
if(x == y)
{
rst[rli] = st[i];
rli++;
}
}
}
int main(int argc, char *argv[])
{
char stbreak[128];
fgets(stbreak, 100, stdin);
char rst[128]; //the string that is being written to
sbreak(stbreak, rst, 2); //2 denotes that the 3rd word will be read
int a = strlen(rst);
for(int i = 0; i < a; i++)
{
printf("%c",rst[i]);
}
return 0;
}
Thanks!
(by the way, im using gcc in emacs to compile)