Help with newline problem

This is a discussion on Help with newline problem within the C Programming forums, part of the General Programming Boards category; Can someone please help me with this assignment: 1. In cryptography, one of the simplest ciphers is known as a ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    24

    Help with newline problem

    Can someone please help me with this assignment:
    1. In cryptography, one of the simplest ciphers is known as a mono-alphabetic shift cipher. In this cipher, each time a letter appears in the original message (called the plaintext), it is replaced by a unique letter from the substitution alphabet, in which the letters are shifted to the right by a certain number of positions. For example, the alphabet may be shifted by 6 positions:
    Original alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
    Substitution alphabet: GHIJKLMNOPQRSTUVWXYZABCDEF

    The resulting message is called the cipher text. To make things more difficult for an
    eavesdropper, punctuation is removed and the cipher text is written in groups of five letters (to
    hide the spaces that indicate individual words). Write a program that will take its input (until
    EOF), remove all non-letters, and then print the cipher text equivalent using the above alphabets.

    Example:

    Enter the message: I came, I saw, I Conquered !,
    Oigsk OygcO Iutwa kxkj
    Make sure the ’shift’ amount is handled as a symbolic constant, so the encryption scheme can be easily changed. Note that the shift works separately for the upper and the lower case alphabets.

    Here's my code thus far:
    Code:
    #include <stdio.h>
    main() 
    {
     int c, d, count=0;
     printf("Enter the Message:");
     do{ c = getchar();  
         
           if (( c >= 'u' && c <='z' ) || ( c >= 'U' && c <= 'Z'))
    	  {d = c-20; putchar(d); count++;}
          
           else
           if (( c >= 'a' && c <= 't' ) || ( c >= 'A' && c <= 'T' ))
              {d = c+6; putchar(d); count++;}
                                          
     
           if ((count&#37;5)==0) printf(" ");
           
         
       } while (c != EOF); /* ^D is EOF*/
     return 0;
    }
    The problem is that when I type in the statement "I came, I saw, I conquered!" I get multiple spaces between every five characters when I only want one space. It has to do with taking newline into account. Can someone tell me how to make my program recognize newline and make it so it doesn't put multiple spaces between every five characters?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's one idea.
    Code:
    #include <stdio.h>
    main() 
    {
     int c, d, count=0;
     printf("Enter the Message:");
     do{ c = getchar();  
         
           if (( c >= 'u' && c <='z' ) || ( c >= 'U' && c <= 'Z'))
    	  {d = c-20; putchar(d); count++;
    	    if ((count%5)==0) printf(" ");
    	   }
          
           else
           if (( c >= 'a' && c <= 't' ) || ( c >= 'A' && c <= 'T' ))
    	{d = c+6; putchar(d); count++;
    	 if ((count%5)==0) printf(" ");
    	}
         
       } while (c != EOF); /* ^D is EOF*/
     return 0;
    }

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably want to make all text upper or lower case (because it gives less hints to a decryptor - not that this sort of thing is particularly hard to crack on statistical basis anyways, but you wouldn't want someone to know where sentences start right?).

    Also, that makes it quite easy to do the whole thing in one step[1]:
    Code:
       c = tolower(getchar());
       if (isalpha(c))
           d = (((c -'a') + 6) &#37; 26) + 'a';
           putchar(d);
           count++;
           if (count % 5 == 0) putchar(" ");
       }
    This replaces the TWO if-statements by using the fact that (n + 26) % 26 == n [where n is smaller than 26].


    [1] Assuming that the input is English and ASCII, we can use this method to maintain the original case - it's only a little bit more complex:
    Code:
       c = getchar();
       if (isalpha(c))
           int t = c & 32;   // if this is 0, it's upper case, if it's 32 it's lower case.
           c &= ~32;  // makes it upper case temporarily.
           d = (((c -'A') + 6) % 26) + 'A' + t;   // t adds in "lowercase" if it was LC to start with.
           putchar(d);
           count++;
           if (count % 5 == 0) putchar(" ");
       }
    --
    Mats
    Last edited by matsp; 10-06-2007 at 03:56 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Cin newline space problem... or something
    By Baqualish in forum C++ Programming
    Replies: 10
    Last Post: 10-17-2007, 04:49 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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