I'm fairly new with arrays but from my understanding it is set up as so:

datatype arrayname[#of elements] = {elements};

I was wondering why this code doesn't work

typedef char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

Any help is appreciated.

Recommended Answers

All 8 Replies

remove the word typedef. I hope you realize that will create a non-null-terminated array of characters. You can't pass it to any of the functions in string.h.

A better and easier way to declare it is like this:

char alphabet[] = "abcdefghijklmnopqrstuvwxyz";

My program is required to use typedef construct to create array types.The goal of the program is to execute a caesar cipher. I planned on making the first array the alphabet, and the second array the alphabet + the shift...

so my goal is to write the array similar to how I wrote in the original post (if that is possible) then shift it. I was thinking of doing the shift in this manner:

alphabet[0+3] (if the shift is 3)

Is this the most efficient way to perform the shift?

by shift do you mean capital letters? if not, then I don't know what you mean by that.

If you must use typedef, then this is how to do it: for more info on typedef read this article.

#include <string.h>

int main()
{
    typedef char alphabet[27];
    alphabet alpha;
    strcpy(alpha, "abcdefghijklmnopqrstuvwxyz");

}

in a caesar cipher letters are shifted by a certain amount to encrypt the messages. For Example:

ABCDEFG shifted by 3 would be DEFGHIJ.

since A would be Alphabet[0] in my array, and D would be Alphabet[3], I figured I would try to add the shift like this :

Alphabet[0+shift]

however I can't even get the Alphabet[] array to even initialize.

If you want to shift the values in an array, you have to be careful what you're doing so that you don't lose anything.

Simplest approach might be to simply write code that shifts by one. Take item from the end being shifted out, store it. Move the item came before it to that place, back up on, copy to the place just vacated, and so on till you take the item from the far end of the array and move it. Place that stored item in the vacant spot ( at the end opposite where it came from.)

Repeat as need for the number of shifts you want to accomplish. It's not the most elegant, but it's sufficient for classwork.

But there are simpler ways of writing Ceaser cipher code than making multiple arrays.

In this program I am supposed to read a text file in which the first line will have an integer (1-25) which determines the shift, so if the text file says 25 I figure the method above will take a long time.

Looping 26 times to move one place, doing that 25 times, is really a trivial amount of work for a current computer - it's only 650 executions of the loop body. Now if it were a 1000 character alphabet you were shifting 500 places, that's starting to be something to be concerned about.

typedef char alphabet[26];

alphabet alpha = {
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 
        'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};

What you could do is iterate through the text that you read in and use a rotate function on each character:

char rotate_char(char ch, int shift)
{
    if (!isalpha(ch))
        return ch;

    // (tolower(ch) - alpha[0]) gives the index into the array
    // modulus sizeof(alpha) ensures that the shifted index is in the array range

    return alpha[((tolower(ch) - alpha[0]) + shift) % sizeof(alpha)];
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.