Thread: very simple string question

  1. #1
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693

    very simple string question

    if I wanted to print out a phrase how would I go about doing that?

    Code:
    int main()
    {
    char string[100];
    
    printf("Enter Phrase\n");
    scanf("%s",string);
    
    printf("%s\n", string);
      
     return 0;   
    }
    this code only allows me to print up until a space comes in to play. Do i have to incorporate some kind of loop?

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    scanf stops at white spaces. You need to use fgets() for that.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your query begs the question: you (seem to) believe that all the things you typed in at the prompt exist in the variable "string". But that is not the case -- your problem isn't with output, but with input. scanf (using %s) will not read in something with a space in it. You need to either ditch the %s in favor of %[, or ditch scanf in favor of fgets.

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    ok i have not been taught fgets yet so i will brush up on that thanks

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by camel-man View Post
    ok i have not been taught fgets yet so i will brush up on that thanks
    Then you should read up on scanf. You can make it read past whitespace. man page (click)

    While it's true that %s stops on white space, you can scanf scan for pretty much anything you want.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by camel-man View Post
    if I wanted to print out a phrase how would I go about doing that?

    Code:
    int main()
    {
    char string[100];
    
    printf("Enter Phrase\n");
    scanf("%[^\n]",string);   <--- note the bold face type...
    
    printf("%s\n", string);
      
     return 0;   
    }
    As already suggested... read the docs for scanf()....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple string question
    By gareth00 in forum C Programming
    Replies: 12
    Last Post: 10-27-2008, 11:19 AM
  2. string simple question
    By -EquinoX- in forum C Programming
    Replies: 1
    Last Post: 04-12-2008, 01:28 PM
  3. simple question on std::string
    By MegaManZZ in forum C++ Programming
    Replies: 8
    Last Post: 01-25-2008, 12:18 PM
  4. Simple string question
    By RobJ in forum C Programming
    Replies: 6
    Last Post: 04-12-2006, 08:19 AM
  5. Simple string question?
    By Cstudent2121 in forum C Programming
    Replies: 7
    Last Post: 12-03-2005, 11:47 PM