• C

# how to use 1D array to do a substitution

hi,
I need to use array for a code, but I don't know how to input an array and make sure that there are exactly 26 letters in that array, and there's no  character is duplicated in the input.

Then I need to use that input as a substitution key, but I don't know how to do that, even it's just a straight substitution.  I think I can use a loop, and let the text that needs to be decoded/encoded as a string, but I am not sure, and if that's the way to do it, I don't know how...

Thanks,
Anthia
###### Who is Participating?

Commented:
short arr[26]={0};//initialize it to zeroes

if(arr[code[i]-'A']!=0) printf("Error");
else arr[code[i]-'A']++;

now say code[0]='A' and code[3]='A';

These are duplicate values.
arr[code[0]-'A']=arr[0].
arr[0]++.

Now arr[0]=1;

arr[code[3]-'A']=arr[0].
arr[0] is not 0.Prints Error.
0

Commented:
You could enter the alphabets directly without getting them from the user.That way,you dont need to check for duplication or array length.
0

Commented:
char array[26];

for(i=0;i<25;i++)
array[i]=i+65;

0

Commented:
Anthia,

This is your homework reworded !! Break down your problem into smaller pieces that you can understand. We are not going to give you the code
0

Author Commented:
the first part is to get the input code, the user can input different code key, so that's why I need to check if the key satisfied the conditions. It's not hard to check for 26, but the check for duplicate part is hard for me.
0

Author Commented:
sunnycoder,
I already did part of the code on my own, but I just don't understand how to do the part that checks the letters. One is checking for the duplicate part, and one is how to check for the plain text and run the 26 times loop to so that when A is the plain text,  it substitutes to another letter.
I am a beginner, so I don't know how to word this properly...sorry..
0

Commented:
>but the check for duplicate part is hard for me.
you need 26 distinct values exactly once ... Obviously you need 26 counters to keep track of which values have you seen how many times

an array of 26 shorts will suffice ... 0th element in that array corresponds to a and 25th element corresponds to z ... Increment the proper index when you input a value .... If any idex becomes 2, show error
0

Commented:
Hi anthia,
> straight substitution
> decoded/encoded

I hope this IS homework and you're not trying to seriously encrypt something this way.

Cheers,
Stefan
0

Commented:
Stefan this IS homework ...
0

Commented:
What about case.Do you want to treat 'a' and 'A' differently?
0

Author Commented:
sunnycoder, thanks, but I still don't really understand your explanation...

for the duplicate part, is it going to be something like this?
for (i=0;i<26;i++)
{ for (j=i+1;j<=n;j++)
{ if (a[i]==a[j])
check='no'
}
}

26 distinct values, is it 'A' = 65, 'B' = 66, something like this, or I need to do something else to assign a value to the characters??

Also, I don't understand the correspondings...
like, if we have 2 array, one is the letters, char letter[26] = {A, B, C, ...., Z}; one is the code, char code[26], i need to get the input for code[26] from the users, then letter[0] will be substituted with code[0], but I don't know how to do this in C language...
by the way, to get the input for the array code[26], can I use
gets(code); ? or scanf should be used??

=========
stefan, it's part of a homework.
0

Author Commented:
ankuratvb,
I just need to convert everything to upper case letter, to simplied the problem.
0

Commented:
use scanf().gets() should be avoided.
0

Commented:
anthia,
> upper case letter
use toupper(). Upper case ASCII codes start with 65 for A, but always use constants like 'A' instead of hard-coded values, such as 65.

>for (i=0;i<26;i++)
>{ for (j=i+1;j<=n;j++)
>{ if (a[i]==a[j])
>check='no'
>}
> }

Possible, but not the best way to do it. The solution Sunny suggested was rather like a "tick-off list": Once you come across, say character number 5 (E), you check in your tick-off array if you already encountered it. If not, use it and place a "tick mark" (set it to 1).

Stefan
0

Commented:
sunnycoder,
> Stefan this IS homework ...
Just joking...

Stefan
0

Commented:
Calculate the difference b/w code[0] and letter[0] and store that in an array.
say int arr[26];

Then,
for e.g. if letter[0]='A' and code[0]='B';
arr[0]=code[0]-letter[0]=1;
i.e. A is to be replaced by B

thus if char c='A';

the encoded would be c+arr[c-65];
0

Commented:
You could use the same array of shorts that sunny suggested for duplication check to store the differences here.
0

Commented:
>check='no'

I didnt know that was valid C.
:~)
0

Author Commented:
Stefan,
thanks for your explanation, I understand the "tick-off list" you are talking about, but can you show me a simply example of that? I always have a problem in programming-- I understand the algorithm, but I don't know how to do that in C.

0

Author Commented:
>You could use the same array of shorts that sunny suggested for duplication check to store the differences here.

0

Commented:
anthia,
> Also, I don't understand the correspondings...
> like, if we have 2 array, one is the letters, char letter[26] = {A, B,
> C, ...., Z}; one is the code, char code[26], i need to get the input
> for code[26] from the users, then letter[0] will be substituted with
> code[0], but I don't know how to do this in C language...

Mathematically, you have a bijective mapping between two alphabets - any encryption should be bijective, otherwise you can't decrypt uniquely.

The C implementation of a mapping is to have an array, like encrypt_mapping[source alphabet char] -> target alphabet char

> by the way, to get the input for the array code[26], can I use
> gets(code); ? or scanf should be used??

Use fgets, not gets. You should check if what you get is a valid permutation of your alphet, just as described above.

Stefan
0

Commented:
anthia,
> Stefan,
> thanks for your explanation, I understand the "tick-off list" you are
> talking about, but can you show me a simply example of that? I always
> have a problem in programming-- I understand the algorithm, but I don't
> know how to do that in C.

Like:

int character_seen[26];

Set all character_seen values to 0.
For each input character already transformed to 0-25 range, look if character_seen[input character]==0.
If yes, fine: set character_seen[input character] to 1 and proceed.
If no, complain that your input permutation is not correct and terminate your program.

Stefan
0

Commented:
0

Author Commented:
One more question on the substitution...
What type should the plain text that needs to be encode/decode be?
Now I know to do the substitution needs to calculate the difference, so what data type should that input be?

I think this is really a stupid question... and THANK YOU VERY MUCH FOR ALL THE HELP YOU GAVE ME!!!
0

Commented:
char.
WHen you use a char in arithmetic,it uses the ASCII value of the char
0

Author Commented:
Oh, I see. in this code, I can use the isalpha() to find out the letters to do the substitution, how about the non-alphabet characters? Do I need to do anything to make sure that it will remain the same in the encode/decode text??

And in my homework, it says
There are three lines: first, the key; second, plain text to be encoded, third, text to be decoded. All lines are read from keyboards.
==========
For this part, do it mean that I need to have a simple function to let the user pick if they want to encode or decode?
Or it means something else?
0

Commented:
>And in my homework, it says
>There are three lines: first, the key; second, plain text to be encoded, third, text to be >decoded. All lines are read from keyboards.

I think it just means that you have to demonstrate both encoding and decoding.
So,using the key,you have to encode one line of text and decode another line of text,
both provided by the user.

You may implement a menu interface if you want.
0

Author Commented:
Thanks VERY MUCH!!  I will split the points. THANK YOU FOR THE HELP!!!
0

Commented:
anthia,
> What type should the plain text that needs to be encode/decode be?

It's a char or unsigned char.

Your check for correctness could also use isupper().

If you want to use this for a different alphabet (like upper/lower case, isprint() characters or the full 8bit character set), you just need a bigger substitution map.

Stefan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.