• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

text encrypting algorithm

/* encrypting program.
we have the Text T[] and the Key K[], and the encrypted text cnew[]
cnew[i]=T[i]+K[i];
in order to produce only latin characters we have:
         if isupper(K) then cnew=T+K-65('A');
         else cnew=T=K-97('a')
         if cnew is not alpha then we print # character and character cnew-26.
         The output I take with this code is "LEMON", i.e. the key in upper case
         and I should have as output:"Lx#fopv e#f rn#hr"
         where is the problem????
*/
#include <stdio.h>
#include <ctype.h>

int main(void){
    char T[]="Attack at dawn";
    char K[]="lemon";
    char cnew[20],temp,c;
    int tindex=0,kindex=0,cindex=0;
   
    while(T[tindex]!='\0'){
         while(K[kindex]!='\0'){
              if (T[tindex]!=' ')                  
                 temp=T[tindex]+K[kindex];
              else {cnew[cindex++]=' ';break;}  
              if (isupper(K[kindex]))              
                 temp=temp-65;
              else if (islower(K[kindex])) temp=temp-97;
              cnew[cindex]=temp;
              if (!isalpha(cnew[cindex])) {
                                          printf("#%c",cnew[cindex]-26);
                                          cindex=cindex+2;
                                          }
              else printf("%c",cnew[cindex++]);
              kindex++;
         }
     tindex++;
     }
     
     c=getchar();
     return 0;    
}
Thanks!!
JIM
             
                                                                                 
   
   
0
dimi67
Asked:
dimi67
  • 2
  • 2
  • 2
1 Solution
 
lucky_jamesCommented:
<<<<< cindex=cindex+2;
why r u using it? you are already incrementing cindex laterwards.......
0
 
lucky_jamesCommented:
oh, seems like you want to keep the '#%c' in cnew itself.
if so, then i dont see any statement here..you are just using printf....that wont do.
0
 
Infinity08Commented:
>>               else {cnew[cindex++]=' ';break;}

You probably want to use continue instead of break. continue goes to the next iteration, while break stops the loop. Also, before the continue, you'll have to increment kindex.

Also : don't you want to print the spaces in the output ?

A better way of doing this is not to use break or continue, but to use an if-else statement to choose which to do when.


>>                  temp=temp-65;

Instead of 65, it's nicer to use 'A', and instead of 97, use 'a'. That way it's easier to see what the values mean ... ie. they are the ASCII values of the characters 'A' and 'a' respectively.


>>          while(K[kindex]!='\0'){

before starting this loop, you probably want to reset kindex to 0 ...



Make these modifications, and try again. There might be other problems, but these should already make it better ... Post your resulting code here and the issues you still have with it, and we'll help you further ...
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.

 
dimi67Author Commented:
ok! I fixed it! The problem was with the inner loop....
0
 
Infinity08Commented:
>>The problem was with the inner loop....

As I said ... ;)

I noticed that you gave a B grade. That usually means that the answer wasn't entirely satisfactory. I'd like to improve myself, so can I ask what was missing ? I also like to mention that you can ask for clarifications if something is not clear.
0
 
dimi67Author Commented:
finally you have rigth!! I m sorry, I was very anxious in order to solve the problem. I finally removed totaly the while(k[kindex]!=NULL) with a if(k[kindex]!=NULL) {do something} else kindex=0;//because we use key circuraly....
I apologise once more...
Jim
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now