Thread: Entering strings with spaces

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    41

    Exclamation Entering strings with spaces

    Hi there, this is my first post, but i can see I'll be writting here often!

    The long and short of it is, as a beginner, i get set tasks by my lefcturer, one of my current tasks is relitively easy compared to some of the others i have, although i have one major problem.

    When i enter the string (as a character array) it considers the spaces in my sentances to be enters, for example the code below

    Code:
    /*
    
    
    /*Program to sort and output the longest of 6 user defined strings*/ /*mitchell kent 323386*/
    
    
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	char lengthystring[6][100];
    	int loop,maxlength,length;
    
    	printf("Longest string finder\n");
    	printf("=-=-=-=-=-=-=-=-=-=-=\n\n");
    
    	printf("Please enter your six sentences, no longer than 20 characters each\n\n");
    
    
    	for(loop=0;loop<=5;loop++)
    	{
    	scanf(" %s",lengthystring[loop]);
    	}
    
    	printf("\n");
    
    	maxlength=strlen(lengthystring[0]);
    
    	for(loop=0;loop<=5;loop++)
    	{
    	length=strlen(lengthystring[loop]);
    	if(length>maxlength)
    		maxlength=length;
    
    	}
    
    	printf("\nYour longest string(s) can be seen below, %d characters in length:\n",maxlength);
    	
    	for(loop=0;loop<=5;loop++)
    	{
    		if(strlen(lengthystring[loop])==maxlength)
    			printf("\n%s\n",lengthystring[loop]);
    	}
    
    	printf("\n");
    	return 0;
    }
    
    */
    when i compile it using microsoft visual c++ (though as .c file) you dont get the full sentance entered as a string, only each word.

    we havent yet learnt how to use pointers, nor any functions for user input other than scanf. I have looked around and have seen the gets() or somethng similar, though have never seen it being used without pointers...

    any help would be greatly appreciated (and this assignment is due in v soon, so any help would be grateful, any quick replies would be fantastic!)

    Yours
    Mitch


    (just to be clear, if you wrote the sentence "hi there my name is mitchell" it would consider that 6 strings of individual words, where as i want that to be one string)
    Cheers!
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    [email protected]
    ~~~~~~~~~~

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    scanf breaks off on whitespace, if you need string data with internal whitespace then you should use fgets:
    Code:
    for(loop=0;loop<=5;loop++)
    {
      if ( fgets ( lengthystring[loop], 100, stdin ) == NULL )
        break;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    41
    Perfect, works a treat now. I have looked up what it is that function does, but its dfescription used pointers aswell, does anyone have a link or quick description of what each parameter does (well the first two are pretty obvious, but the stdin?)

    anyway, many thanks for sucha speedy reply! i can get on with the harder tasks now!

    Mitch
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    [email protected]
    ~~~~~~~~~~

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    As prelude has already explained in the other thread, you can use scanf as follows,

    Code:
     
    for(i=0;i<=5;i++)
    {
      scanf(" %[^\n]",lengthystring[i])
    }
    %[^\n] indicates accept all characters till you find a '\n'. So i guess that will do.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    sorry about that 'lengthystring[i]' missing

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    41

    cheers!

    I tried both methods and they both worked!

    Thank you both, cause not only does it mean i have got my program to work, I've learnt two new tricks.

    Dont worry, i'll keep my posts coming, my next project due in for fri morn and i have to write a help system, using searches within strings etc, once again all without pointers!

    I'll be back soon

    Mitch
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    [email protected]
    ~~~~~~~~~~

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > As prelude has already explained in the other thread, you can use scanf as follows,
    And Prelude also explains in another thread why fgets() is better than scanf(" %[^\n]"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading strings with spaces
    By dezz101 in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 06:02 AM
  2. reading strings with spaces using sscanf
    By kotoko in forum C Programming
    Replies: 2
    Last Post: 06-14-2008, 05:06 PM
  3. Strings allowing spaces?
    By Dan17 in forum C++ Programming
    Replies: 13
    Last Post: 02-04-2006, 03:15 PM
  4. Getting scaf to accept strings with spaces in them?
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 11-12-2005, 09:57 AM
  5. Entering strings into a list?
    By chadsxe in forum C++ Programming
    Replies: 10
    Last Post: 06-02-2005, 12:40 PM