I have a nested for loop of
for(int i = 0; i < strlen(key); i++)
{
for(int j = i + 1; j < strlen(key); i++)
{
if(toupper(key[i]) == toupper(key[j]))
{
printf("cipher must have all unique characters\n");
return 1;
}
}
}
I've checked in debug50 and it tells me that
for(int j = i + 1; < strlen(key); i++)
just goes back to itself rather than going back to the parent loop and adding 1 to i before going back to the j loop which means it's just telling me that the second letter is equal to the second letter. My full code is here:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if(argc != 2)
{
printf("please enter two arguments in command-line\n");
return 1;
}
string key = argv[1];
for(int i = 0; i < strlen(key); i++)
{
if (!isalpha(key[i]))
{
printf("please use only letters\n");
return 1;
}
if (strlen(key) != 26)
{
printf("cipher has to be 26 characters\n");
return 1;
}
}
for (int i = 0; i < strlen(key); i++)
{
for (int j = i + 1; j < strlen(key); i++)
{
if(toupper(key[i]) == toupper(key[j]))
{
printf("cipher must have all unique characters\n");
return 1;
}
}
}
string plaintext = get_string("plaintext: ");
for(int i = 0; i <strlen(key); i++)
{
if(islower(key[i]))
{
key[i] = key[i] - 32;
}
}
printf("cipher: ");
for(int i = 0; i < strlen(plaintext); i++)
{
if(isupper(plaintext[i]))
{
int letter = plaintext[i] - 65;
printf("%c", key[letter]);
}
else if(islower(plaintext[i]))
{
int letter = plaintext[i] - 97;
printf("%c", key[letter]);
}
else (printf("%c", plaintext[i]));
}
printf("\n");
}
any ideas on what I can do?