2

I am very new to programming and i wonder if there is a way to print out the first word of a string with gets() in C?

void printFirstWord(char string[])
{
    int i;

for(i = 0; i < (string[i] != '\0'); i++)
    {
    if(isalpha(string[i]))
        printf("%c", string[i]);
    }


}

int main()
{

char string[MAX];
printf("Type in a scentence");
gets(string);
printFirstWord(string);


return 0;
}

This is the function that i have written and called in main right now. Is it because i have isalpha in the function?

  • what does the code do? what were you expecting it to do? – skrrgwasme Sep 30 '16 at 22:36
  • 6
    `gets()` doesn't print *any* words. Also, `gets()` is obsolete and no longer standard C. – Dmitri Sep 30 '16 at 22:37
  • In `printFirstWord`, break out of the `for` loop when you get to the end of the first word. – jxh Sep 30 '16 at 22:38
  • Also, `i < (string[i] != '\0')` doesn't make much sense... how about just `isalpha(string[i])` in the loop test, and get rid of the `if` inside? – Dmitri Sep 30 '16 at 22:39
  • Why are you using `gets()` at all? It was removed from the language because it was inherently broken. – Jesin Sep 30 '16 at 22:41
  • Sorry, my bad! What i meant was, is there a way after you type in a string with gets() to just print out the first word of that string with a printf? What the function does now is that is prints out only one word of the string. So what i should use instead of gets is Scanf("%s", string); ? – Linus Sens Ingels Sep 30 '16 at 22:44
  • 2
    `scanf("%s",string);` will only read the first word anyway (and should have the max length added into the format string to avoid overrunning the buffer). If you want to read the whole line still, try `fgets()`. – Dmitri Sep 30 '16 at 22:49
  • 2
    Please read about [Why `gets()` is too dangerous to be used — ever](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used). – Jonathan Leffler Sep 30 '16 at 23:00
  • Will do, thanks for all the help! – Linus Sens Ingels Sep 30 '16 at 23:02

2 Answers2

1

In your implementation, you might add the following line in the loop:

if (string[i] == ' ')
 break;

also, fix your loop parameters e.g. like this:

for (i = 0; i <  strlen(string); i++)

Overall implementation in you way will be as below. Consider choosing another design according to comments you got, e.g. not using gets.

void printFirstWord(char string[])
{
    int i;

    for (i = 0; i <  strlen(string); i++)
    {
        if (isalpha(string[i]))
            printf("%c", string[i]);

        if (string[i] == ' ')
            break;
    }


}

int main()
{
#define MAX 100
    char string[MAX];
    printf("Type in a scentence\n");
    gets_s(string, MAX);
    printFirstWord(string);

    getchar();
    return 0;
}
Eric Abramov
  • 462
  • 4
  • 19
-1

I just found a way with isblank(); function, hope it helps to anybody :)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main (){
int length, number, counter, position;
char name[50];
printf("Please type your complete name:\n");
gets(name);
//strlen();
//Returns the length of the given null-terminated byte string, that is, the number of characters in a character array
length=strlen(name); 
    //Counts each position until it finds a space
   for(counter=0;counter<length;counter++)
   {
            if(isblank(name[counter]))
                      position=counter;
   }
   //Prints each character until the counter reaches the position number given by the counter variable
   printf("\nThe first word you typed is: ");
   for(number=0; number<=position; number++){
    printf("%c", name[number]);
   }

  }
MarlonDSC
  • 189
  • 3
  • 4