Simple Encryption Program in C

In summary: CODE ] [ /CODE ]...around your code so that whitespace and indentation are preserved. Secondly, you might want to check to see if c is a vowel by using if (c==a||c==e||c==i||c==o||c==u||c==A||c==E||c==I||c==O||c==U) . If it is a vowel, you can substitute it with the corresponding letter Z. However, if it is not a vowel, you just output the received character as is.In summary, the code reads in a sentence up to the period, replaces each vowel with the letter 'Z', and prints the modified sentence with
  • #1
stellina
8
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
  • #2


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:
  • #3


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


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


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.
 
  • #6
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:
  • #7
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.
 
  • #8
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:
 
  • #9
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!
 

1. What is an encryption program and why is it important?

An encryption program is a software or algorithm that transforms plain text into a coded form known as ciphertext. It is important because it ensures the confidentiality and security of sensitive information, such as personal data, passwords, and financial transactions.

2. How does an encryption program work?

An encryption program uses a set of mathematical operations and algorithms to scramble plain text into an unreadable form. The encryption process involves a key, which is a unique code used to encrypt and decrypt the message. The encrypted message can only be decoded with the correct key.

3. Can any programming language be used to create an encryption program?

Yes, any programming language can be used to create an encryption program. However, some languages may have built-in libraries or functions that make it easier to implement encryption algorithms. In the case of c programming, there are several libraries available for encryption, such as OpenSSL and Crypto++.

4. What are the different types of encryption algorithms used in c programming?

There are several types of encryption algorithms used in c programming, including symmetric key encryption (e.g., AES, DES, and 3DES) and asymmetric key encryption (e.g., RSA and ECC). Hashing algorithms (e.g., SHA-256 and MD5) are also commonly used for data integrity and authentication.

5. Is encryption 100% secure?

No, encryption is not 100% secure. While strong encryption algorithms can make it extremely difficult for hackers to break the code, it is not impossible. Factors such as the length and complexity of the key, the strength of the algorithm, and proper implementation also play a role in the security of an encryption program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
880
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
983
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
851
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
Back
Top