?
Solved

Problem with one dimensional array when using <ctype.h> library

Posted on 2003-03-18
7
Medium Priority
?
195 Views
Last Modified: 2010-04-15
Hi there!

This is the first time I'm writing, and I'm having a
problem now. I'm writing a program capable of decode a
secret message using a really simple code. I want to
be able to "decode" a character when the user types
any letter, and to count the number of lines on the
secret message. The key I'm using is:

a=Z b=Y c=X d=W e=V etc... and of course the viceverse
of that would be A=z B=y C=x D=w E=v etc...

Notice that if the user types a lower case letter, I
want to display its corresponding upper case letter in
order to decifrate the secret message that the user
sends me as an input. Same for the other way: If the
user types an upper case letter I want to display its
corresponding lowercase letter, and go on...

PROBLEM:
When testing my program, I'm getting a wrong output
(but it's almost correct) for some reason. I'm at the
point where I can't think clearly now, so I need your
help.
For example, one of the text files I'm using as a
test-input is called "text2" and has the following
text inside:

gSRH RH HLNV
GVCG.  xZM BLF
IVZW RG?

The corresponding output for the text2 file, should be
the decoded message:

This is some
text.  Can you
read it?

But I'm getting this! :

ghis is some
text.  xan you
read it?


NOTE: I'm running my program with the following
command:

> a.out < text2


This is some of the code I have wrote at this point:

========================================================

#include <stdio.h>
#include <ctype.h>

main()
{

int c, i, abc[26]={0}, nl_cnt=0;

while ( (c=getchar()) != EOF) {

   if (isalpha(c)) {

      if (islower(c)) {
         c = 'Z' - (c-'a');
         ++abc[c - 'A']; }

      if (isupper(c)) {
         c = 'z' - (c-'A');
         ++abc[c - 'a']; }
    }
   
   putchar(c);
         
   if (c == '\n') {
      printf("\n");
      nl_cnt++; }
         
}                                          
   
return 0;
}

========================================================

Please advice.
Sincerely,

-gino
0
Comment
Question by:quco74
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8160754
Post all the code -

Your code:

     if (islower(c)) {
        c = 'Z' - (c-'a');
        ++abc[c - 'A']; }

If you enter 'g', the statement

        c = 'Z' - (c-'a');

will yield c = 'T', which is what you want...
0
 
LVL 6

Expert Comment

by:gj62
ID: 8160761
Somewhere you are not correctly translating from lower-case, at least that is what your sample output indicates, though the code above looks like you are.

What platform are you on?  Have you tried stepping through with a debugger?  Should be pretty quick and easy to find...
0
 

Author Comment

by:quco74
ID: 8161117
I'm using a UNIX plataform, but I don't have any debugger available (I'm a beginner). I found out something interesting also: If I change the order of my two if statements, putting the if(isupper(c)) first and then if(islowwer(c)) second, I will get:

> a.out < text2
TSRH RH HLNV
GVCG.  CZM BLF
IVZW RG?

Then the translation for uppercase is working fine, but the transaltion for lowercase is not!

What could it be?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Expert Comment

by:gj62
ID: 8161145
The problem is you are changing 'c', then retesting.  Use a temp var instead of c to manipulate, you can set them equal after the tests...

e.g.

char tempChar;

tempChar = c;
    if (islower(c)) {
        tempChar = 'Z' - (c-'a');
        ++abc[tempChar - 'A']; }

    if (isupper(c)) {  
        tempChar = 'z' - (c-'A');
        ++abc[tempChar - 'a']; }
c = tempChar;
0
 
LVL 6

Expert Comment

by:gj62
ID: 8161150
If that is not it, you will need to post all the code...
0
 
LVL 6

Accepted Solution

by:
gj62 earned 200 total points
ID: 8161277
Sorry, much easier just to use 'else'

 if (islower(c)) {
       tempChar = 'Z' - (c-'a');
       ++abc[tempChar - 'A']; }

  else if (isupper(c)) {  /*notice the ELSE statement*/
       tempChar = 'z' - (c-'A');
       ++abc[tempChar - 'a']; }

My brain is in a bit of fog again today...

To be explicit, what is happening is that when your character = 'g', your first if statement was executed, changing the character to a 'T', which is what you want.

However, it then gets caught by the very next if statement that tests if c is upper (which it now is) and changes 'T' back to 'g'...

The "else" statement will prevent this from happening...
0
 

Author Comment

by:quco74
ID: 8161554
You are right!
Now it works! Thank you so much!!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question