The following code is broken when trying to run due to an issue replacing the character on line 33. Am I replacing the character in the string incorrectly?
The code is designed to encrypt lowercase characters in the *cat string. Each character in code2 is 'mapped' to a character in the same position in code1. The lowercase chars in *cat are replaced with their substituted char from code2.
//Ben Adamson
//v1.0
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
void code(char *s);
int main()
{
char *cat = "The cat sat";
code(cat);
_getch();
return 0;
}
void code(char *s)
{
char code1[] = "abcdefghijklmnopqrstuvwxyz";
char code2[] = "bpduhijkaltxwmrzfoysvngeqc";
char *letter;
unsigned int i, letterpos;
for(i=0; i<strlen(s); i++)
{
if(isalpha(s[i]) && islower(s[i]))
{
letter = strchr(code1, s[i]);
letterpos = (int)(letter - code1);
s[i] = code2[letterpos];
}
}
printf("New string is %s", s);
}