2

Here is the code, basically I gotta do the ceasar cipher in C using arrays and chars only, here is what I have so far:

#include <stdio.h>
#include <stdlib.h>


main ()
{
    char alfabeto[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'};
    int a=0;
    int b=0;
    int m=0;
    int c=0;
    //char letra;
    int cont=0;
    char k[]={};


    printf("Introduzca un primer numero 'a':\n");
    scanf(" %i", &a);
    printf("Introduzca un segundo numero 'b':\n");
    scanf(" %i", &b);

    printf("Introduzca una palabra clave entre 4 y 10 letras (cuando termine presione '.'):\n");    

    //Falta una validacion para la palabra.

        for (int j=0;j<10;j++)
        {
            scanf(" %c",&k[j]);
            cont=j; //cuenta la cantidad de elementos
            if (k[j]=='.')
            {
                j=10;
            }
        }

        for(int j=0;j<cont;j++) //Error in this bit I think.
        {
            for (int i=0;i<26;i++)
            {
                if (alfabeto[i]==k[j])
                {
                    m=i;
                    i=26;
                }
            }
                c = ( (a * m) + b );
                printf("%c es: %i \t",k[j],c);
        }
}

It uses a formula where c=(a*m+b).

m being the position of the original letter for example: A=0 then m=0.

a being a number you choose as well as b.

In my case I chose a=1 and b=3 and the word CESAR as the key word.

According to the formula the output number of each letter should be c.

The output should be:

C es: 5     E es: 7     S es: 21     A es: 3     R es: 20

Yet it is this:

WrongOutput

UPDATE:

I had to correct this:

char k[]={};

Into this:

char k[10]={'\0'};

Now I get the right output:

Right Output

  • 1
    The compiler has no possible way of knowing the size of this array `char k[]={};` and it generates a compiler error. Even if another compiler made it 0 or 1 length, you won't input any data here. – Weather Vane Mar 31 '16 at 20:15
  • Thank you @user3121023 it fixed the error! –  Mar 31 '16 at 20:37
  • Thank you @WeatherVane it makes sense :D –  Mar 31 '16 at 20:37
  • @Constanza so did this answer your question? If not, please update the question. – Scolytus Mar 31 '16 at 21:10
  • 1
    This is not how you update a question, Constanza, just put **EDIT** at the end explaining the fix/putting the corrected code. Because now, since you changed it, it is not very clear what the error was. – Dimitar Mar 31 '16 at 22:08
  • Side note, it is now illegal to omit return type of function. Please use `int main()`. – n. m. could be an AI Apr 01 '16 at 17:21

0 Answers0