Jump to content

C Program to input a users string

Go to solution Solved by Travercraig,

I read some slides based on a c programming book and found this:

post-225550-0-30008700-1453066431_thumb.

I have this simple string program in C that reads a users input and outputs it to a console window.

 

However for some reason if a user enters a string with a space in, it only outputs the first word, and terminates after the first space.

 

As shown in the image:

 

Here is the code:

#include <stdio.h>int main(){    char str[80];			printf("Enter a string less than 80 characters: \n");		scanf("%s", str);		printf("Your string is: %s\n", str);	    return 0;}

post-225550-0-57842500-1453050612.png

   
   
Link to comment
https://linustechtips.com/topic/529089-c-program-to-input-a-users-string/
Share on other sites

Link to post
Share on other sites

 

I have this simple string program in C that reads a users input and outputs it to a console window.

 

However for some reason if a user enters a string with a space in, it only outputs the first word, and terminates after the first space.

 

As shown in the image:

 

Here is the code:

#include <stdio.h>int main(){    char str[80];			printf("Enter a string less than 80 characters: \n");		scanf("%s", str);		printf("Your string is: %s\n", str);	    return 0;}

i know nothing about c, but what i do know from other programming languages, that sometimes inputs with a space in it need to be tables and then every word is a new string in the table

example:

hello there worldis converted to:input (whatever you want) = ['hello', 'there', 'world']in lua, should be the same in c i think
Link to post
Share on other sites

#include <stdio.h>#include <stdlib.h>#include <string.h>#define SIZE 80int main(){    char *c=(char*)malloc(SIZE);    printf("Enter a string less than 80 characters: \n");    fgets(c, SIZE , stdin);    if ((strlen(c)>0) && (c[strlen (c) - 1] == '\n'))        c[strlen (c) - 1] = '\0';    printf("%s" , c);    free(c);    return 0;}

Also , for the future :

fgets is a better choice than scanf.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

I don't have a cpp compiler on hand, but if memory serves, 'space' is considered white space - and cpp won't output it

it treats it similarly to EOL (enter), TAB and others

you should google around for bypassing this issue

Well you are almost right. The function scanf and it's derivations stop string at spaces (if you're not using the [] parameter)

There is a way to reconfigure it with [ ] this option but because of its possibility to help stack smashing it is highly recommended to use fgets with a buffer or fgetc in a loop. 

I hope this could help somehow. :)

Link to post
Share on other sites

Well you are almost right. The function scanf and it's derivations stop string at spaces (if you're not using the [] parameter)

There is a way to reconfigure it with [ ] this option but because of its possibility to help stack smashing it is highly recommended to use fgets with a buffer or fgetc in a loop. 

I hope this could help somehow. :)

I haven't coded somethin in over 17y, I'm just remembering stuff at this point :P
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×