Thread: How to handle spaces in strings

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    15

    How to handle spaces in strings

    Hi everyone,

    I'm writing code that uses an encoding called rot13, where each letter is shifted 13 spaces, so "a" becomes "n" and "b" becomes "o" etc. It was an old C assignment.

    The problem:

    When I enter Hello world, only hello is converted and not world.

    Any ideas?

    Code:
    /*
    Description: ROT13 encoding; to change each letter of a word 13 places along the alphabet. i.e, a
    becomes n.
    Input is from keyboard
    modified date:26/11/04
    */
    
    #include <stdio.h>
    //#include <CLIB.H>     Commented out because it produces errors
    #include <string.h>
    main(){
       char rot13[100];
       int i=0;
           
       printf("Enter word  n ");
       scanf("%s",rot13);
    
       for(i=0;i<(strlen(rot13));i++)
       {
                                     
           if (('a'<=rot13[i])&&(rot13[i]<='n'))
           {
                   rot13[i]= rot13[i] + 13;
           }
           else if (('m'<=rot13[i])&&(rot13[i]<='z'))
           {
                           rot13[i]= rot13[i] - 13;
           }
           else if (('A'<=rot13[i])&&(rot13[i]<='N'))
           {
                           rot13[i]= rot13[i] + 13;
           }
           else if (('N'<=rot13[i])&&(rot13[i]<='Z'))
           {
                           rot13[i]= rot13[i] - 13;
           }                   
           else if (rot13[i]== 32)                                 // 32 is ACSII for space
           {
                            rot13[i] = ' ';
                         
           } 
        }
    
       printf("%s\n",rot13);
       system("PAUSE");                                            // There is a better away to stop the program from quiting, not sure what it is
     }

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Maybe try fgets instead of scanf.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Epy View Post
    Maybe try fgets instead of scanf.
    Or...
    Code:
       char rot13[100];
       int i=0;
           
       printf("Enter word  n ");
       scanf("%99[^\n]",rot13);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    Thanks Epy.

    I haven't been able to find a good example of fgets and as far as I can see it only referes to reading data from files. Can you point me to a good example of fgets?

    I'll keep looking into it. Thanks.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    Brilliant hk_mp5kpdw, that worked.

    I'm not sure what, 99[^\n] does though.

    "%s", must only accept a string untill a space is pressed, so 99[^\n] must accept 99 charaters?

    edit: %s is a type of Format Specifier for scanf:

    C - printf and scanf, Format Specifiers
    Last edited by a_satari; 04-05-2010 at 07:48 AM.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    %[ABCD........XYZ] causes only the characters inside the brackets to be read by scanf ; if any other character is found then it terminates.

    Oppositely in case of %[^\n]
    the circumflex(^) causes every character to be read except the character inside the brackets, which becomes the terminating character.here \n is the terminating character.

    So its a useful way for scanf sometimes.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by a_satari View Post
    I'm not sure what, 99[^\n] does though.
    C - printf and scanf, Format Specifiers
    Because that documentation is unfortunately half-assed, and fails to mention a number of format specifiers such as the rather important []. Eg, to capture an alphanumeric string (no spaces, no punctuation):
    Code:
    scanf("%[a-zA-Z0-9]", string_ptr);
    An example of fgets for user input:
    Code:
    char input[256];
    fgets(input,256,stdin);
    fgets() is very useful in combination with sscanf() -- notice, two ss -- since you can simply get all the input into a string, extract what you want from the string, and the then discard the rest, rather than messing around using scanf() on input directly.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with setting text into LTEXT using Window Handle
    By jasperleeabc in forum Windows Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Strings allowing spaces?
    By Dan17 in forum C++ Programming
    Replies: 13
    Last Post: 02-04-2006, 03:15 PM
  3. accepting long strings with spaces
    By trang in forum C Programming
    Replies: 2
    Last Post: 01-05-2004, 04:17 PM
  4. c-style string vs. c++-style strings
    By Mbrio in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 12:26 PM
  5. array of strings + more
    By null in forum C Programming
    Replies: 10
    Last Post: 10-01-2001, 03:39 PM

Tags for this Thread