Extra printed stmts...why?

This is a discussion on Extra printed stmts...why? within the C Programming forums, part of the General Programming Boards category; This is a program that encrypts & then decrypts a given string - there should be only 3 printed statements ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    3

    Question Extra printed stmts...why?

    This is a program that encrypts & then decrypts a given string - there should be only 3 printed statements (plaintext, encrypted text & decrypted text)...(never mind it's not formatted to not overflow on the screen, I'll deal with that later)

    Here's the problem:
    I'm getting 5 printed statements & I can't see the reason why...
    This may be something really obvious but I'm at a loss (first semester C prog student)

    ...if anybody can shed some light on this I would really appreciate it!

    ----------------------Here's my code-----------------------
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define MAXCHARS 200
    #define MAXLINE 60

    void decrypt(const char in[], char out[]);
    void encrypt(const char in[], char out[]);
    void printtext(const char t[]);
    char scramble(const char c);
    char unscramble(const char c);
    void key(const char c, int n);

    int main(void)
    {
    const char plaintext[] = "Fourscore and seven years ago our fathers \
    brought forth upon this continent a new nation, conceived in liberty, \
    and dedicated to the proposition that all men are created equal.";

    char decrypttext[sizeof(plaintext)/sizeof(char)];
    char encrypttext[sizeof(plaintext)/sizeof(char)];

    printtext(plaintext); //print original text

    encrypt(plaintext, encrypttext);
    printtext(encrypttext); //print encrypted text

    decrypt(encrypttext, decrypttext);
    printtext(decrypttext); //print decrpyted text

    //test to show plaintext == decrypttext
    if(strcmp(decrypttext, encrypttext) == 0)
    printf("Identical Strings\n");
    else printf("Not Identical Strings\n");

    return 0;
    }

    //translate plaintext to encrypttext
    void encrypt(const char in[], char out[])
    {
    int k;
    for(k = 0; in[k] != '\0'; k++)
    {
    scramble(in[k]);
    strcpy(out, in);
    }
    printf("\n");
    return;
    }

    //translate encrypttext to decrypttext
    void decrypt(const char in[], char out[])
    {

    int k;
    for(k = 0; in[k] != '\0'; k++)
    {
    unscramble(in[k]);
    strcpy(out, in);
    }
    printf("\n");
    return;
    }

    //translate a plain char to encrypted char
    char scramble(const char c)
    {
    key(c, 4);
    return c;
    }

    //translate encrypted char to a plain char
    char unscramble(const char c)
    {
    key(c, 5);
    return c;
    }

    //function for key
    void key(const char c, int n)
    {
    if(n == 4)
    {
    switch(c)
    {
    case 'a': putchar(113); break;
    case 'b': putchar(97); break;
    case 'c': putchar(122); break;
    case 'd': putchar(120); break;
    case 'e': putchar(115); break;
    case 'f': putchar(119); break;
    case 'g': putchar(101); break;
    case 'h': putchar(100); break;
    case 'i': putchar(99); break;
    case 'j': putchar(118); break;
    case 'k': putchar(102); break;
    case 'l': putchar(114); break;
    case 'm': putchar(116); break;
    case 'n': putchar(103); break;
    case 'o': putchar(98); break;
    case 'p': putchar(110); break;
    case 'q': putchar(104); break;
    case 'r': putchar(121); break;
    case 's': putchar(117); break;
    case 't': putchar(106); break;
    case 'u': putchar(109); break;
    case 'v': putchar(107); break;
    case 'w': putchar(105); break;
    case 'x': putchar(111); break;
    case 'y': putchar(108); break;
    case 'z': putchar(112); break;
    case 'F': putchar(87); break;
    default: putchar(' '); break;
    }
    }
    else
    {
    switch(c)
    {
    case 'q': putchar(97); break;
    case 'a': putchar(98); break;
    case 'z': putchar(99); break;
    case 'x': putchar(100); break;
    case 's': putchar(101); break;
    case 'w': putchar(102); break;
    case 'e': putchar(103); break;
    case 'd': putchar(104); break;
    case 'c': putchar(105); break;
    case 'v': putchar(106); break;
    case 'u': putchar(107); break;
    case 'j': putchar(108); break;
    case 'm': putchar(109); break;
    case 'k': putchar(110); break;
    case 'f': putchar(111); break;
    case 'r': putchar(112); break;
    case 't': putchar(113); break;
    case 'g': putchar(114); break;
    case 'b': putchar(115); break;
    case 'n': putchar(116); break;
    case 'h': putchar(117); break;
    case 'y': putchar(118); break;
    case 'i': putchar(119); break;
    case 'o': putchar(120); break;
    case 'l': putchar(121); break;
    case 'p': putchar(122); break;
    case 'W': putchar(70); break;
    default: putchar(" "); break;
    }
    }
    }


    void printtext(const char t[])
    {
    printf("%s\n", t);
    return;
    }

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    You call printtext three times. This is the regular output that you see. You also call encrypt and decrypt each once. The encrypt and decrypt functions do not actually change the data, but instead loop through the string and output the encrypted or decrypted character to the screen using the putchar(). This is why there are five outputs: three regular, one encrypted, one decrypted.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    3

    printing stmts...

    Thanks David -
    now it makes sense as to 'why' I'm getting the extra statements but I'm confused on how to eliminate this problem - if I don't use putchar() in the switch statements how else can I transfer the values of each character (I originally had printf() in the switch stmt but I think that was producing the same output)

    I get the feeling I'm overlooking a simpler way to do this...


    thanks

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I made a bunch of changes, and commented out the original. I also had to change your decrypt, as it wasn't quite right.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define MAXCHARS 200
    #define MAXLINE 60

    void decrypt(const char in[], char out[]);
    void encrypt(const char in[], char out[]);
    void printtext(const char t[]);
    char scramble(const char c);
    char unscramble(const char c);
    char key(const char c, int n);

    int main(void)
    {
    const char plaintext[] = "Fourscore and seven years ago our fathers \
    brought forth upon this continent a new nation, conceived in liberty, \
    and dedicated to the proposition that all men are created equal.";

    char decrypttext[sizeof(plaintext)/sizeof(char)];
    char encrypttext[sizeof(plaintext)/sizeof(char)];

    printtext(plaintext); //print original text

    encrypt(plaintext, encrypttext);
    printtext(encrypttext); //print encrypted text

    decrypt(encrypttext, decrypttext);
    printtext(decrypttext); //print decrpyted text

    //test to show plaintext == decrypttext
    if(strcmp(decrypttext, plaintext) == 0)
    printf("Identical Strings\n");
    else printf("Not Identical Strings\n");

    return 0;
    }

    //translate plaintext to encrypttext
    void encrypt(const char in[], char out[])
    {
    int k;
    for(k = 0; in[k] != '\0'; k++)
    {
    //scramble(in[k]);
    out[k] = scramble(in[k]);
    //strcpy(out, in);
    }
    out[k] = '\0';
    printf("\n");
    return;
    }

    //translate encrypttext to decrypttext
    void decrypt(const char in[], char out[])
    {

    int k;
    for(k = 0; in[k] != '\0'; k++)
    {
    //unscramble(in[k]);
    out[k] = unscramble(in[k]);
    //strcpy(out, in);
    }
    out[k] = '\0';
    printf("\n");
    return;
    }

    //translate a plain char to encrypted char
    char scramble(const char c)
    {
    return key(c, 4);
    //key(c, 4);
    //return c;
    }

    //translate encrypted char to a plain char
    char unscramble(const char c)
    {
    return key(c, 5);
    //key(c, 5);
    //return c;
    }

    //function for key
    //void key(const char c, int n)
    char key(const char c, int n)
    {
    if(n == 4)
    {
    switch(c)
    {
    /*
    case 'a': putchar(113); break;
    case 'b': putchar(97); break;
    case 'c': putchar(122); break;
    case 'd': putchar(120); break;
    case 'e': putchar(115); break;
    case 'f': putchar(119); break;
    case 'g': putchar(101); break;
    case 'h': putchar(100); break;
    case 'i': putchar(99); break;
    case 'j': putchar(118); break;
    case 'k': putchar(102); break;
    case 'l': putchar(114); break;
    case 'm': putchar(116); break;
    case 'n': putchar(103); break;
    case 'o': putchar(98); break;
    case 'p': putchar(110); break;
    case 'q': putchar(104); break;
    case 'r': putchar(121); break;
    case 's': putchar(117); break;
    case 't': putchar(106); break;
    case 'u': putchar(109); break;
    case 'v': putchar(107); break;
    case 'w': putchar(105); break;
    case 'x': putchar(111); break;
    case 'y': putchar(108); break;
    case 'z': putchar(112); break;
    case 'F': putchar(87); break;
    default: putchar(' '); break;
    */
    case 'a': return(113);
    case 'b': return(97);
    case 'c': return(122);
    case 'd': return(120);
    case 'e': return(115);
    case 'f': return(119);
    case 'g': return(101);
    case 'h': return(100);
    case 'i': return(99);
    case 'j': return(118);
    case 'k': return(102);
    case 'l': return(114);
    case 'm': return(116);
    case 'n': return(103);
    case 'o': return(98);
    case 'p': return(110);
    case 'q': return(104);
    case 'r': return(121);
    case 's': return(117);
    case 't': return(106);
    case 'u': return(109);
    case 'v': return(107);
    case 'w': return(105);
    case 'x': return(111);
    case 'y': return(108);
    case 'z': return(112);
    case 'F': return(87);
    default: return(' ');
    }
    }
    else
    {
    switch(c)
    {
    /*
    case 'q': putchar(97); break;
    case 'a': putchar(98); break;
    case 'z': putchar(99); break;
    case 'x': putchar(100); break;
    case 's': putchar(101); break;
    case 'w': putchar(102); break;
    case 'e': putchar(103); break;
    case 'd': putchar(104); break;
    case 'c': putchar(105); break;
    case 'v': putchar(106); break;
    case 'u': putchar(107); break;
    case 'j': putchar(108); break;
    case 'm': putchar(109); break;
    case 'k': putchar(110); break;
    case 'f': putchar(111); break;
    case 'r': putchar(112); break;
    case 't': putchar(113); break;
    case 'g': putchar(114); break;
    case 'b': putchar(115); break;
    case 'n': putchar(116); break;
    case 'h': putchar(117); break;
    case 'y': putchar(118); break;
    case 'i': putchar(119); break;
    case 'o': putchar(120); break;
    case 'l': putchar(121); break;
    case 'p': putchar(122); break;
    case 'W': putchar(70); break;
    default: putchar(' '); break;
    */
    case 'q': return(97);
    case 'a': return(98);
    case 'z': return(99);
    case 'x': return(100);
    case 's': return(101);
    case 'w': return(102);
    case 'e': return(103);
    case 'd': return(104);
    case 'c': return(105);
    case 'v': return(106);
    case 'f': return(107);
    case 'r': return(108);
    case 't': return(109);
    case 'g': return(110);
    case 'b': return(111);
    case 'n': return(112);
    case 'h': return(113);
    case 'y': return(114);
    case 'u': return(115);
    case 'j': return(116);
    case 'm': return(117);
    case 'k': return(118);
    case 'i': return(119);
    case 'o': return(120);
    case 'l': return(121);
    case 'p': return(122);
    case 'W': return(70);
    default: return(' ');
    }
    }
    }


    void printtext(const char t[])
    {
    printf("%s\n", t);
    return;
    }

  5. #5
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Sorry I wasn't able to get back to you again yesterday, but it looks like swoopy has already posted a solution. Basically, after a few adjustments to the way the functions are handled, you just want to return the appropriate value to be assigned into the array instead of outputting to the screen.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. buffer has extra contents in gtk+ 2.0
    By MK27 in forum Linux Programming
    Replies: 5
    Last Post: 08-04-2008, 12:57 PM
  2. adding extra to char*
    By sujeet1 in forum C Programming
    Replies: 2
    Last Post: 10-17-2007, 01:21 PM
  3. Output file contains extra data
    By nizbit in forum C Programming
    Replies: 3
    Last Post: 02-21-2005, 08:00 PM
  4. extra word printing
    By kashifk in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2003, 05:03 PM
  5. From where these two extra bytes came?
    By Juganoo in forum C Programming
    Replies: 4
    Last Post: 12-24-2002, 05:32 PM

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