Solved

how to use 1D array to do a substitution

Posted on 2004-04-21
29
314 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:anthia
  • 11
  • 9
  • 6
  • +1
29 Comments
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10886110
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10886120
char array[26];

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

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10886127
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 Comment

by:anthia
ID: 10886167
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 Comment

by:anthia
ID: 10886199
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
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 20 total points
ID: 10886206
>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
 
LVL 12

Expert Comment

by:stefan73
ID: 10886223
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10886229
Stefan this IS homework ...
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10886282
What about case.Do you want to treat 'a' and 'A' differently?
0
 

Author Comment

by:anthia
ID: 10886286
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 Comment

by:anthia
ID: 10886289
ankuratvb,
I just need to convert everything to upper case letter, to simplied the problem.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10886311
use scanf().gets() should be avoided.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10886332
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
 
LVL 12

Expert Comment

by:stefan73
ID: 10886337
sunnycoder,
> Stefan this IS homework ...
Just joking...

Stefan
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 9

Expert Comment

by:ankuratvb
ID: 10886351
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10886396
You could use the same array of shorts that sunny suggested for duplication check to store the differences here.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10886407
>check='no'

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

Author Comment

by:anthia
ID: 10886412
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 Comment

by:anthia
ID: 10886431
>You could use the same array of shorts that sunny suggested for duplication check to store the differences here.

Sorry... kind of confused about this... Can you explanation a little bit more for me?
0
 
LVL 12

Assisted Solution

by:stefan73
stefan73 earned 30 total points
ID: 10886472
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
 
LVL 9

Accepted Solution

by:
ankuratvb earned 75 total points
ID: 10886479
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
 
LVL 12

Expert Comment

by:stefan73
ID: 10886498
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10886564
0
 

Author Comment

by:anthia
ID: 10886578
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10886592
char.
WHen you use a char in arithmetic,it uses the ASCII value of the char
0
 

Author Comment

by:anthia
ID: 10886663
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10886703
>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 Comment

by:anthia
ID: 10886725
Thanks VERY MUCH!!  I will split the points. THANK YOU FOR THE HELP!!!
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10886811
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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now