Hi my friends , I have a simple C question for you
how can I get firstname and lastname in one string ?
Hi my friends , I have a simple C question for you
how can I get firstname and lastname in one string ?
use an array of characters and put a space between the names?
I suppose you've only used scanf's %s specifier for string input. The usual recommendation for full lines (rather than words) is fgets:
Code:char buf[BUFSIZ]; if (fgets(buf, sizeof buf, stdin) != NULL) fputs(buf, stdout);
My best code is written with the delete key.
first put the first name in a string than the last name in another string and mix them together using "strcpy" and "strcat"
Code:strcpy(mixed, name1); strcat(mixed, " "); strcat(mixed, name2);
Last edited by Bennie98; 11-02-2010 at 08:40 AM.
Because Tetris Is Unrealistic.
"The fear of death is the most unjustified of all fears, for there's no risk of accident for someone who's dead." - Albert Einstein
"The Edge... there is no honest way to explain it because the only people who really know where it is are the ones who have gone over." - Hunter S. Thompson
"I never think of the future. It comes soon enough." - Albert Einstein
If you read using
scanf("%s",str)
it will stop reading at first white space character(tab,space or enter)
But if you need to read a line of text use any of following
gets(str)
OR
scanf("%[^\n]s",str)
NOTE:str is the name of string,which is a pointer to first character of string
Hope this helped...........
You might want to point out what is wrong with it since it looks okay to me.Originally Posted by CommonTater
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
>why are you saying that gets() and scanf() are insecure?
gets is 100% unsafe because there's no way to avoid a buffer overflow. gets will stop writing to the array when it runs out of characters form the stream, even if the array isn't large enough to handle it. scanf is 90% unsafe because so few people understand that the %s specifier is no different from gets unless a field width is included to limit the input. linuxlover's scanf line can be made safe in terms of buffer overflow:
>Ummmm... check your bracketing Prelude....Code:char str[1024]; scanf("%1023[^\n]", str);
I'll go out on a limb and assume you meant that the braces are missing:
But that's a matter of style. I've never found redundant braces to save me from bugs, so I don't bother with them.Code:char buf[BUFSIZ]; if (fgets(buf, sizeof buf, stdin) != NULL) { fputs(buf, stdout); }
My best code is written with the delete key.
>Nope... sizeof(buf)
Meh, so much for my psychic ability. But oddly enough, the same answer applies somewhat. Parens are only required when the operand to sizeof is a type name.
My best code is written with the delete key.