• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

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