What's Wrong with My Simple Encryption Program in C?

Click For Summary

Discussion Overview

The discussion revolves around a user's C program intended for simple encryption by removing spaces and replacing vowels with 'Z'. Participants provide feedback on the code, identify potential issues, and suggest improvements.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant points out that the variable declarations for vowels (e.g., char a, e, i, o, u;) are incorrectly initialized to 0, which leads to incorrect comparisons later in the code.
  • Another participant suggests that the initial getchar() call may not serve a useful purpose and questions the logic behind incrementing the ASCII value of characters.
  • There is a recommendation to check for vowels using character literals (e.g., if (c == 'a')) instead of using the previously defined variables.
  • Participants highlight the importance of proper code formatting using code tags for better readability.
  • Suggestions are made to restructure the while loop to ensure characters are printed correctly based on conditions for spaces and vowels.
  • One participant mentions the need to consider the return value of getchar, which is an int, and not just a char, to handle EOF correctly.

Areas of Agreement / Disagreement

Participants generally agree on the issues present in the code and provide suggestions for improvement. However, there is no consensus on a single corrected version of the program, as multiple approaches are discussed.

Contextual Notes

Limitations include unresolved issues regarding the handling of spaces and vowels, as well as the correct placement of output statements within the loop. The discussion does not resolve the final implementation details.

Who May Find This Useful

Readers interested in programming in C, particularly those learning about character manipulation and basic encryption techniques, may find this discussion beneficial.

stellina
Messages
8
Reaction score
0
Hi guys!

I am writing a program that reads in a single sentence up to the period. The program outputs the modified sentence with all of the spaces removed and replaces each of the vowels with the letter 'Z'.

Here is my code but I am stuck and I don't understand what's wrong, can anyone help?Thanks!


#include <stdio.h>
#define SPACE ' '

int main(void)
{
char c;
char a,e,i,o,u;
char A,E,I,O,U;
char Z;

c = getchar();

a=e=i=o=u=0;
A=E=I=O=U=0;
Z = 0;



printf("Enter text to be analyzed:");
while ((c= getchar() !='.'))
{
if (c == SPACE)
putchar(c+1);

if (c == a|| c == e|| c == i|| c == o|| c== u|| c== A|| c==E||c== I|| c== O|| c== U)
putchar(Z);



}

putchar(c);
return 0;
}
 
Physics news on Phys.org


Welcome to PF stellina!

You should try enclosing code in [ CODE ] [ /CODE ] tags (without the spaces). It will preserve whitespace/indentation.

Let's make sure you understand what you are doing. A declaration like
Code:
 char a;
will define a variable of type char whose name is a. Later on you set a equal to 0. Let's be clear: this means that the value of the variable named a is 0. So, when you do your test
Code:
 if (c == a)
, you are basically saying,
Code:
 if (c == 0)
. I don't think that this is the test you want to be doing.

What you want to be doing is checking whether the obtained character c is a lowercase 'a'. The hard way to do this is to check to see whether c is equal to the number that is the ASCII code value for lowercase a. A much easier way to do this is to say
Code:
 if ( c == 'a')
, where the single quotes indicate that you are referring to the character a (i.e. 'a' automatically evaluates to the correct ASCII value).

EDIT: Another thing I'm wondering: what is the purpose of your very first c = getchar(); statement? It doesn't seem like it would do anything useful.

EDIT 2: I also don't understand what the purpose of c = putchar(c+1); is. Basically you are incrementing character c's ASCII value by 1, which means that the character you will output will be totally different from the character you read in. If your intention is just to output spaces as received, then you don't need to do anything special with them. EDIT 5: Whoops I didn't read your post carefully enough. You want to remove the spaces, which means you could just not output anything upon receipt of a space. I.e. if (c == SPACE) { [do nothing] }.

EDIT 3: The putchar(Z); statement won't output an uppercase Z for the same reasoning as above. Z is a variable that you defined and set equal to 0. Therefore, putchar(Z) will output a character whose ASCII code is equal to 0 (which happens to be the NULL character, as you can see here: http://www.asciitable.com/).

EDIT 4: The final putchar(c); statement should be within the while loop, since you want to output a character every time one is received. However, it should only occur if no substitutions need to happen, which means that your while loop should be of the form:

Code:
if ([statements that check to see if c is a vowel]) {
  [statement that outputs an uppercase Z];
} else putchar(c);

This means that if the received character is a vowel, you substitute a Z for it, otherwise (i.e. "else") you just output the character as received.
 
Last edited:


Thank you so much! things are clearer now. the program runs, however I don't get the encrypted output...any ideas?
 


to print the new line I added putchar(c); before the return statement but it doesn't work.
 


Can you post the new version in its entirety?

You might need to type:

while ( (c=getchar()) != '.' )

Because what you have now might be interpreted as:

while ( c = (getchar() != '.'))

which is totally different and not what you want. C has operator precedence rules that define which operators are evaluated first in an expression.
 
Well, firstly, you should put code tags around your code so that it is formatted properly. As it is, we can't see the indentation, which makes it very hard to read. Put [ code] before your code and [ /code] after it, removing the spaces I put in after the square brackets.

I've done it for you this time:
Code:
#include <stdio.h>
#define SPACE ' '

int main(void)
{
    char c;
    char a,e,i,o,u;
    char A,E,I,O,U;
    char Z;
    
    c = getchar();
    
    a=e=i=o=u=0;
    A=E=I=O=U=0;
    Z = 0;

    
       
    printf("Enter text to be analyzed:");
    while ((c= getchar() !='.'))
    {    
        if (c == SPACE)
           putchar(c+1);
        
        if (c == a|| c == e|| c == i|| c == o|| c== u|| c== A|| c==E||c== I|| c== O|| c== U)
            putchar(Z); 
        
    
    
       } 
    
    putchar(c);
    return 0;
}

You also should always tell us exactly what's wrong. Are you getting an error message at compile? Is the output wrong? Don't make us guess.

In your case, it seems that last putchar(c) is completely in the wrong place. I'd suggest this kind of structure:
Code:
if (c == SPACE)
{

}
else if (c == a || ... etc ...)
{

}
else
{
    putchar(c);
}

Make sense? As it is, it handles spaces and vowels, but only tries to print the output character after it's finished the looping. Clearly, you want that in the while loop so each character that is not handled is printed.

You're also not checking for vowels correctly. It's not (c == a || c == o || ... etc). Those a, o, u, etc variables won't work. You need to check against the character a, not some variable called a. So it's, like the space, (c == 'a' || c == 'o' || ... etc).

You should look at using the toupper or tolower functions so you don't have to check for both the upper and lower case characters. Just turn it into an upper or lower case character and compare against that.

You should also look more closely at the return value of getchar. It actually returns an unsigned char cast to an int, not just a char. It returns an EOF on error or end of file, and that's why it returns an int and not just an unsigned char. toupper/tolower work on ints as well.

Hope that helps.

EDIT: Darnit, wrong forum and double post. I see you already got answers in the other forum. Might be a good idea to petition a moderator to move the thread instead of posting it again in another forum next time so I don't waste time on already answered questions. I had no way of knowing this was homework.
 
Last edited:
Hi Grep- thanks a lot for your comments! Sorry about the double posting , is the first time in my life that I use a live forum and I am still figuring out how it works.
 
stellina said:
Hi Grep- thanks a lot for your comments! Sorry about the double posting , is the first time in my life that I use a live forum and I am still figuring out how it works.

Two threads merged. Yes, please do not multiple post, it is against the rules (see the Rules link at the top of the page).

Welcome to the PF, BTW. :smile:
 
stellina said:
Hi Grep- thanks a lot for your comments! Sorry about the double posting , is the first time in my life that I use a live forum and I am still figuring out how it works.

hehe No big problem, you're not the first, or the last. As long as people learn when it's pointed out to them, it's ok. For one, really, I should have some kind of macro for that "Please use code tags" speech, I type it so often. :biggrin:

Thanks for the merge, berkeman!
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K