View Full Version : [PROGRAMMING] Need help with C
bulletmagnet
12-10-2012, 04:58
I've been taking an online course and trying to write a cipher programme in C. But I have spent weeks on this and it's keeping me awake at night and away from my gaming time. What I want is to supply a keyword, e.g. Gamer...and have each letter converted to an number such as...'A' & 'a' = 0; 'B' & 'b' = 1; and so on. Here is what I have but it just doesn't seem to work properly.
int length = strlen (k);
for(int j = 0; j < length; j++)
{
if(isupper(k[j]))
{
k[j] = k[j] - 'A';
}
if(islower(k[j]))
{
k[j] = k[j] - 'a';
}
}
iamthesgt
12-10-2012, 09:53
You can simplify the program by converting your input argument to lowercase first, then only handling lower case options.
char myChar = tolower(k[j]);
I'm a bit confused at what you're trying to do, though. You're just making a simple substitution code? (A=1, B=2, Z=26)?
bulletmagnet
12-11-2012, 13:11
Was able to solve it without help..I'm trying to write a vigenere cipher.
bulletmagnet
12-16-2012, 11:15
Actually I thought I was finished with this thing, but it looks like I am not. I'm trying to write a code for a Vigenere cipher which uses a keyword to encipher some text. What I have to do first is to take the uppercase alphabet and lowercase alphabet and associate them with a number.
A + a = 0
B + b = 1
C + c = 2
...
Z + z = 26
So then with the keyword FRAK:
F=5, R=17, A=0, K=10
Then this key word is applied to my text phrase I want to encrypt. "Frakking toaster lover."
The keyword should apply to each letter in turn shifting it the appropriate number of spaces and when it reaches the end wrapover:
Frakking toaster lover.
FRAKFRAK FRAKFRA KFRAK
So the resulting text should come out as:
Kiaupznq yfacyvr vtmeb
Kiauksdz buawcbr qimrk
Is what is outputted instead.
So what is happening is that only the first four letters are being encrypted properly
and the key word isn't wrapping at the end.
int length = strlen(key);
for(int i = 0; i < length; i++)
{
if(isupper(key[i]))
{
key[i] = key[i] - 'A';
}
else if(islower(key[i]))
{
key[i] = key[i] - 'a';
}
if(i >= length)
{
i = 0;
}
}
Here is the relevant part of my code so you can see what is or isn't going on.
Powered by vBulletin® Version 4.2.3 Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.