reading sentence from stdin

This is a discussion on reading sentence from stdin within the C Programming forums, part of the General Programming Boards category; I want to apply the reverse function to each word that the user enters, for example when I input: God ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    reading sentence from stdin

    I want to apply the reverse function to each word that the user enters, for example when I input:

    God bless me

    It should return

    doG sselb em

    the current code just keeps on waiting for input from stdin, what I want is to terminate right after I print enter

    Code:
    int main()
    {
    	char word[100];
    	while(1){
    		scanf("%s", word);
    		reverse(word);
    		printf("%s ", word);	
    	}
    
    	printf("\n");
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    With a char array[], you could do something like:

    use getchar(), and have the while((array[i++]=getchar()) != ' '), which should signal the end of each word. Then decrement i while you print out the char's, back to i = zero.



    .

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    are you saying I should set i first to the length of the string?

    is there no other easy way to do this, like my code above does the job.. but it just don't know when to stop..

    keeps on reading from stdin, while I only want it to read from stdin only once, which is till I press enter

    I mean I know how to do this the hard way, but I am challanged to do this with as less effort as I can using only the provided C functions
    Last edited by -EquinoX-; 03-28-2009 at 05:28 PM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    while (1) tests for the truth of 1, which one is always true -- so we call while (1) an infinite loop. The usual way out of an infinite loop is to use break.

    There is nothing in the OP to break the infinite loop.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by MK27 View Post
    while (1) tests for the truth of 1, which one is always true -- so we call while (1) an infinite loop. The usual way out of an infinite loop is to use break.

    There is nothing in the OP to break the infinite loop.
    I am aware of that as well... that's why I am asking what should I change so that when the user presses enter then it breaks out of the loop. Or maybe another off topic question.. is there a function in C that could extract words by words from a line of strings?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Alright: if I understand you correctly, you are having a problem grabbing an input string with spaces in it using scanf, so you figured you could get around that by calling scanf over and over?

    That is a Bad Hack (as opposed to a good hack, also possible, so don't feel bad for trying, because if scanf were the only input command available in C, you might be on the right track). But what you really want here is fgets:
    Code:
    #include <stdio.h>
    
    int main() {
    	char buffer[128];
    	puts("Enter a sentance and hit enter");
    	fgets(buffer,128,stdin);  
    	printf("%s",buffer);
    	return 0;
    }
    This will take a whole line, spaces, punctuation, the newline, etc, and add a null terminator (a la the documentation, fgets will supposedly stop reading at 127 bytes, one less than the size in the argument, to allow for the null terminator it adds).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by -EquinoX- View Post
    are you saying I should set i first to the length of the string?

    is there no other easy way to do this, like my code above does the job.. but it just don't know when to stop..

    keeps on reading from stdin, while I only want it to read from stdin only once, which is till I press enter

    I mean I know how to do this the hard way, but I am challanged to do this with as less effort as I can using only the provided C functions
    No.

    Code:
    //something like this:
    char goOn;
    i = 0;
    do   {
       if(c == '\n')  {
          while(i)  { 
             printf("%c", CharArray[i]);
             CharArray[i--] = '\0';
          }
       }
       else  {
          CharArray[i++] = c;
          continue;
       }   
       i = 0;
       printf("\nEnter Another Word [y/n] ? ");
       scanf(" %c", &goOn);
    }while((goOn != 'n' && goOn != 'N') )
    This is completely untested code, and appears more than you want, anyway. Perhaps better, would be to use scanf() to get a whole string? scanf("%200s", charArray);

    Instead of a word, you get the whole sentence. Make Array large so it can't overflow it within reason.

    That would simplify the above.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Quote Originally Posted by Adak View Post
    No.

    Code:
    //something like this:
    char goOn;
    i = 0;
    do   {
       if(c == '\n')  {
          while(i)  { 
             printf("%c", CharArray[i]);
             CharArray[i--] = '\0';
          }
       }
       else  {
          CharArray[i++] = c;
          continue;
       }   
       i = 0;
       printf("\nEnter Another Word [y/n] ? ");
       scanf(" %c", &goOn);
    }while((goOn != 'n' && goOn != 'N') )
    This is completely untested code, and appears more than you want, anyway. Perhaps better, would be to use scanf() to get a whole string? scanf("%200s", charArray);

    Instead of a word, you get the whole sentence. Make Array large so it can't overflow it within reason.

    That would simplify the above.
    what would I do after getting the whole string? I want to reverse each words as I encounter it... basically when the program starts I want it to show out , please enter the string to be reversed.. then the user enters the string and hit enter and then it shows the string that is reversed... if I use fgets then I'll get the string.. but I'll have to decompose it per words by words

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 2
    Last Post: 07-12-2008, 12:00 PM
  3. value of stdin (not zero?)
    By taisao in forum Linux Programming
    Replies: 3
    Last Post: 05-27-2007, 08:14 AM
  4. Reading Input From stdin
    By EvilGuru in forum C Programming
    Replies: 5
    Last Post: 11-25-2005, 12:48 AM
  5. Array, reading in response etc...
    By mattz in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 10:41 AM

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