text encrypting algorithm

Posted on 2007-10-21
Last Modified: 2010-04-15
/* encrypting program.
we have the Text T[] and the Key K[], and the encrypted text cnew[]
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;
              if (T[tindex]!=' ')                  
              else {cnew[cindex++]=' ';break;}  
              if (isupper(K[kindex]))              
              else if (islower(K[kindex])) temp=temp-97;
              if (!isalpha(cnew[cindex])) {
              else printf("%c",cnew[cindex++]);
     return 0;    
Question by:dimi67
    LVL 7

    Expert Comment

    <<<<< cindex=cindex+2;
    why r u using it? you are already incrementing cindex laterwards.......
    LVL 7

    Expert Comment

    oh, seems like you want to keep the '#%c' in cnew itself.
    if so, then i dont see any statement are just using printf....that wont do.
    LVL 53

    Accepted Solution

    >>               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 ...

    Author Comment

    ok! I fixed it! The problem was with the inner loop....
    LVL 53

    Expert Comment

    >>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.

    Author Comment

    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...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now