how can I get firstname and lastname in one string ?

This is a discussion on how can I get firstname and lastname in one string ? within the C Programming forums, part of the General Programming Boards category; Hi my friends , I have a simple C question for you how can I get firstname and lastname in ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    LA
    Posts
    1

    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 ?

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    437
    use an array of characters and put a space between the names?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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.

  4. #4
    C-no_Ob Bennie98's Avatar
    Join Date
    Oct 2010
    Location
    Ledeberg, Ghent, East-Flanders, Belgium
    Posts
    49
    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

  5. #5
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52

    Smile

    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...........

  6. #6
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    Quote Originally Posted by linuxlover View Post
    But if you need to read a line of text use any of following
    gets(str)
    OR
    scanf("%[^\n]s",str)
    I disagree with this advice as both gets() and scanf() are insecure functions. Prelude had the best advice in my opinion.

  7. #7
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    Quote Originally Posted by matrixx333 View Post
    I disagree with this advice as both gets() and scanf() are insecure functions. Prelude had the best advice in my opinion.



    Why we get warning messages while compiling gets function.
    why are you saying that gets() and scanf() are insecure?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Prelude View Post
    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);
    Ummmm... check your bracketing Prelude....

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by CommonTater
    Ummmm... check your bracketing Prelude....
    You might want to point out what is wrong with it since it looks okay to me.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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:
    Code:
    char str[1024];
    
    scanf("%1023[^\n]", str);
    >Ummmm... check your bracketing Prelude....
    I'll go out on a limb and assume you meant that the braces are missing:
    Code:
    char buf[BUFSIZ];
    
    if (fgets(buf, sizeof buf, stdin) != NULL) {
        fputs(buf, stdout);
    }
    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.
    My best code is written with the delete key.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Prelude View Post
    >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:
    Code:
    char str[1024];
    
    scanf("%1023[^\n]", str);
    >Ummmm... check your bracketing Prelude....
    I'll go out on a limb and assume you meant that the braces are missing:
    Code:
    char buf[BUFSIZ];
    
    if (fgets(buf, sizeof buf, stdin) != NULL) {
        fputs(buf, stdout);
    }
    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.
    Nope... sizeof(buf)

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Abnormal program termination
    By Kayoss in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2006, 03:29 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. Desperate for help - ugly nested if
    By baseballkitten in forum C Programming
    Replies: 4
    Last Post: 11-19-2001, 02:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21