nvteighen
March 23rd, 2008, 03:54 PM
Hi there!
I've been experimenting these days with some C just because of fun and need some help.
This code tries to substitute one single letter into a number (A=0, B=1, C=2, etc.), but for some reason it doesn't work and gets stuck into an infinite loop. This is the first step for a little Caesar Cipher encoder, where letters are "summed" by a key. So, I need to convert letters into numbers first. (I don't want any help on the rest)
#include <stdio.h>
#include <string.h> //for strlen();
#include <ctype.h> //for toupper();
char *abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //Alphabet defined as global variable
int subst(char x); //Function defined
int main(void)
{
char key;
int key2;
puts("Enter a 1 character long text");
scanf("%c", &key);
key = toupper(key); //converts all letters to uppercase
key2 = subst(key); //calling substitution function
printf("%d\n", key2);
return 0;
}
int subst(char x)
{
int out, i, len;
len = strlen(abc); //do len be abc string's length
for((i = 0); (i = len); i++)
{
if(abc[i] == x)
{
out = i;
}
/*This should search each abc's characters and compare it with x (which is the key variable) and, if they're the same, the function will return the corresponding character's place in the array as result (so, A = 0, B = 1, etc.)*/
}
return out;
}
Thanks!
I've been experimenting these days with some C just because of fun and need some help.
This code tries to substitute one single letter into a number (A=0, B=1, C=2, etc.), but for some reason it doesn't work and gets stuck into an infinite loop. This is the first step for a little Caesar Cipher encoder, where letters are "summed" by a key. So, I need to convert letters into numbers first. (I don't want any help on the rest)
#include <stdio.h>
#include <string.h> //for strlen();
#include <ctype.h> //for toupper();
char *abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //Alphabet defined as global variable
int subst(char x); //Function defined
int main(void)
{
char key;
int key2;
puts("Enter a 1 character long text");
scanf("%c", &key);
key = toupper(key); //converts all letters to uppercase
key2 = subst(key); //calling substitution function
printf("%d\n", key2);
return 0;
}
int subst(char x)
{
int out, i, len;
len = strlen(abc); //do len be abc string's length
for((i = 0); (i = len); i++)
{
if(abc[i] == x)
{
out = i;
}
/*This should search each abc's characters and compare it with x (which is the key variable) and, if they're the same, the function will return the corresponding character's place in the array as result (so, A = 0, B = 1, etc.)*/
}
return out;
}
Thanks!