0

I declared one variable to track the position of the text and a variable to track the position of key. But my program is giving extra characters.

This is my For Loop

    for(int i = 0, n = strlen(text); i < n; i++)
{
    for(int j = 0; j < i; j++)
    {
        if(isalpha(text[i]))
        {
            if(isupper(text[i]))
            {
                if(isupper(argv[1][j]))
                {
                    printf("%c",((text[i] - 65) + (argv[1][j] - 65)%26 + 65));
                }
                else if(islower(argv[1][j]))
                {
                    printf("%c",((text[i] - 65) + (argv[1][j] - 97)%26 + 65));
                }
            }
            else if(islower(text[i]))
            {
                if(isupper(argv[1][j]))
                {
                    printf("%c",((text[i] - 97) + (argv[1][j] - 65)%26 + 97));
                }
                else if(islower(argv[1][j]))
                {
                    printf("%c",((text[i] - 97) + (argv[1][j] - 97)%26 + 97));
                }
            }

        }
        else
        {
            printf("%c", text[i]);
        }
    }
}

Key: AAAA

text: ABCDEF

Output: ABBCCCDDDDEEEEFFFFF

2 Answers 2

1

The problem is that you are using two loops, trying to control the index on the key (j) with a loop. You can't control the indexing of the key with a loop, there's just no way to do so.

Remember that when you have one loop inside the other, the inner loop is going to execute i * j times! You don't want to cycle through the key more times than necessary.

This is a case where you want to keep it simple. Think about how the key is used. When you encode a letter of plaintext, you increment the index of the key once. When you process a non-letter in the plaintext, you don't do anything with the index of the key. The simple way to do so is to just create an integer variable and only increment it when you encode a letter. When you get to the end of the key, reset the var to 0. No need for a loop.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • Ok I removed the inner loop, then declared a variable j = 0 Then inside the for loop of i, I added an If(j > strlen(argv[1][j]) { j = 0; } But the key still won't wrap around
    – Aminekh
    Dec 12, 2015 at 13:14
  • argv[1][j] is a single character in the argv[1] string. Does that give you any ideas?
    – Cliff B
    Dec 12, 2015 at 16:45
0

I can see your inner for loop is iterating i no. of times which in case incrementing by 1 on each iteration of outer for loop, that's why you're getting one A on first iteration, two B on second iteration, three C on third iteration... on each iteration of outer for loop. Hope this makes sense.

5
  • Can you give me more information on how to fix it?
    – Aminekh
    Dec 11, 2015 at 16:59
  • probably you don't need two loops as you're repeating the same stuffs. Dec 11, 2015 at 17:12
  • ok I changed the second for loop for(int i = 0, j = 0, n = strlen(text); i < n; i++, j++) But now I still get some strange characters at the end
    – Aminekh
    Dec 11, 2015 at 17:27
  • what characters? Dec 11, 2015 at 17:39
  • I got the same problem here.. They keyword is not looping again.. So the beginning is ok but I got strange characters in the end.. Did anyone found the answer to this yet?
    – Kiwi
    Jan 14, 2016 at 16:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .