0

Here is my code. Substitution Cipher in C. But i got an error this line: char *encryption (char cipher_text[]) { function definition is not allowed here. I think probably "main" function place not right. How can i fix it? And by the way how can i generate random alphabet for this code? Thank you so much.

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
#include <string.h>


char *encryption (char cipher_text[]) {
            int i, val, j;
            printf("\n abcdefghijklmnopqrstuvwxyz \n");  
misshyde
  • 45
  • 1
  • 8
  • you can't nest functions.. – Eugene Sh. Nov 23 '16 at 14:37
  • if you want to generate a random alphabet, look into using the `rand()` function - you would want to generate a random number and mod it by 26, then add 97 to it, and store the variable in a `char`. See http://www.asciitable.com/index/asciifull.gif for why we add 97 – schil227 Nov 23 '16 at 14:45
  • `srand((unsigned int) time(NULL) ); char key = rand( ) % 26 + 'a';` i added this lines for random generate alphabet but i got an error : `cipher_text[i]=key[j];` subscripted value is not an array, pointer or vector. How can i fix it? – misshyde Nov 23 '16 at 15:57

2 Answers2

0

You cannot define a function inside another one. encryption is defined in main :/

µtex
  • 900
  • 5
  • 12
Eliot B.
  • 162
  • 11
0

In C, you cannot declare a function inside another function, like you did. Here is your code that will compile:

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
#include <string.h>

char *encryption (char []);
void *decryption (char []);
char alpha [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'};
char key[26];


char *encryption (char cipher_text[]) {
int i, val, j;
printf("enter the unique KEY of 26 character:");
scanf("%s", key);
printf("\n abcdefghijklmnopqrstuvwxyz \n");
printf ("%s", key);

for (i=0; i <strlen(cipher_text); i++) 
{
    for (j=0; j<26; j++)
    {
        if (alpha[j]== cipher_text[i]) {
            cipher_text[i]=key[j];
            break;
        }
    }
}

printf ("your message enc: %s", cipher_text);
return cipher_text; 
}

int main ()
{
    int i, key, choice, flag=0;
    char *c_text, msg[255];
    printf("\n Enter plain text:");
    scanf ("%[^\n]", msg);
    encryption(msg);
    return 0;
}

How to generate random characters is answered here.

Community
  • 1
  • 1
Honza Dejdar
  • 947
  • 7
  • 19
  • thanks but i still got errors (using Xcode in OS X ) `char *cipher_text, msg[255];` (not c_text btw) this line says: unused variable ciphertext. program is working but just i entered the plaintext then program exit. :/ it didn't encrypt. Thank you so much. – misshyde Nov 23 '16 at 15:03
  • Well, you have to call the function from main. I've just added it to the code i posted. – Honza Dejdar Nov 23 '16 at 15:07
  • Are you sure you copied the code exactly? With the line almost at the end with the function call (`encryption(msg);`)? It works for me. – Honza Dejdar Nov 23 '16 at 15:15
  • ok, now working but i got errors about values :/ thank you so much. i will checked this encryption is true or false. – misshyde Nov 23 '16 at 15:22
  • No problem. You can accept this answer, if it answered your question. – Honza Dejdar Nov 23 '16 at 15:23
  • ok, thank you so much Honza. and can i ask a question for decryption after i write codes? how can i contact with you? :) – misshyde Nov 23 '16 at 15:30
  • I don't think there are private messages here on SO, so I suggest you ask in a new question. – Honza Dejdar Nov 23 '16 at 15:33
  • One last question: `srand((unsigned int) time(NULL) ); char key = rand( ) % 26 + 'a';` i added this lines for random generate alphabet but i got an error : `cipher_text[i]=key[j];` subscripted value is not an array, pointer or vector. How can i fix it? – misshyde Nov 23 '16 at 15:55
  • You are trying to acces single `char` `key` as an array using `[]` operator. Try it like this: `cipher_text[i]=key;` – Honza Dejdar Nov 23 '16 at 15:58
  • i added new answer you see the my whole code, then i will delete. – misshyde Nov 23 '16 at 16:03