How to receive a sentence from a user input in C?

Viewed 92

I'm trying to create a small program using C programming. Essentially while running in the console, the program should receive a sentence from a user.

Deliverables:

  1. I want to be able to ask users their name
  2. prompt the user to enter a write a short sentence
  3. output a user name and their sentence prompt
  4. when the user types into the text area and presses Enter, it will Submit.

**Not sure if I have to create an HTML file to connect my c file to the form for the submit action. Below is what I have made so far. Thank you!

#include <stdio.h>
  
int main()
{
  char word[100];
  char name;

  printf("what is your name? ");
      scanf("%c", &name);
    
  printf("Enter your sentence: ");
    
    scanf("%s", word);
    printf("Output : %s", word);
    return 0;
}
3 Answers

To get user input either use gets_s() or fgets().

#include <stdio.h>
  
int main()
{
  char name[100];
  printf("what is your name? ");
  gets_s( name, sizeof(name) );
    
  char sentence[100];
  printf("Enter your sentence: ");
  gets_s( sentence, sizeof(sentence) );

  printf( "Hi %s! You wrote: %s\n", name, sentence );
  return 0;
}

I prefer fgets() because you can determine whether or not you read the user’s entire line by whether or not a newline at the end of the input string.

I am uncertain how you intend to use this with an HTML form. Are you messing with a server CGI program? Do you have Apache or something else useful set up to test it all with? Is this for employment or personal?

[edit] As per commentary and continuing from above, fgets() is my typical go-to solution. However, it needs some help. At the very minimum you must check for and remove the newline. A helper function is useful:

char * getline( FILE * f, char * s, size_t n )
{
  if ((n < 1) or !fgets( s, n, f )) return NULL;
  char * p = strchr( s, '\n' );
  if (p) *p = '\0';
  else
  {
    // The input line is larger than (n-1), meaning
    // that the entire line of input was not read --
    // only the part of it that fit in `s` was read.
    // The way you deal with this condition is up to you.
  }
  return s;
}

I personally prefer a solution that allocates and returns the input buffer (within a reasonable size limit, say, 4K bytes). That is overkill for a lot of stuff, though.

The code below seems close to what I'm looking for, but I cannot get the exclamation mark to show after the user's name and not join with the user's sentence. For example ...

Hi, username! You Wrote: I love programming.

#include <stdio.h>

int main(){
  char name[100];
  printf("Hi, What is your name ? ");
  fgets( name, sizeof(name),stdin);

  char sentence[100];
  printf("Enter you sentence: ");
  fgets( sentence,sizeof(sentence),stdin);

  printf( "Hi %s You Wrote: %s\n", name, sentence);
  return 0;
}

In your code you have enterd %c in name but it will only accept one character of the entered string but instead of this if you will user %s then it will accept complete string untill whitespace character. So In order to accept complete string with whitespace character you have to write %[^\n] or %[^\n]s.

#include<stdio.h>
int main(){
char word[100], name[50];
printf("What is your name ?");
scanf("%[^\n]",name);
printf("Enter your statement : ");
scanf(" %[^\n]",word);
printf("Output : %s",word);
return 0;
}

Method : 2

If you want to use another method to accept user input as string you have to add include one library of c which is <String.h> and then you can use gets function to accept complete string from the user.

Code :-

#include<stdio.h>
#include<string.h>
int main(){
char word[100], name[50];
printf("What is your name ?");
gets(name);
printf("Enter your statement : ");
gets(word);
printf("Output : %s",word);
return 0;
}
Related