2

I am about to make an encryption of a textstring, using another textstring as a "key" for my encryption. It is basically just a reorganization of the ASCII-characters.

The key is given and is structured in a bad way, requiring some extra programming. The ASCII-characters are listed in this key with one charachter for each line, and the corresponding encrypted characters two indexes away.

This is the example of "key.txt":

A g
B 9
C ü
D (
E z

...continuing for all ASCII-characters. An encryption in my program would therfore result in:

"EDABEDA" -> "z(g9z(g"

When I am doing the encryption.

  1. I let the program take in an input-string and I create another string with the encryption-key-characters.

  2. I go through each character in the input string with a for-sling. For each character in the input string, I check if there is a matching character in my encryption-key string. In the encryption key I jump 4 steps at a time since I am going to encrypt and only have to compare A,B,C,D...

  3. I use strcmp() to find a match. And when there is a match, when the character in the input string is the same as in the encryption key, I write the encrypted character to the output string. The encrypted character is placed two indexes ahead of the main character in the key-string.

  4. The warnings occur in the strcmp()

Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 3000

void encryption(char *input);

int main()
{
    char input[] = "EDABEDA";
    encryption(input);
}

void encryption(char *input)
{
    int length,i,k;
    size_t result;
    char dicc[1000];
    char output[SIZE];
    FILE *f;

    f = fopen("key.txt","r");

    fseek(f, 0, SEEK_END);
    length = ftell(f);
    fseek(f, 0, SEEK_SET);
    result = fread(dicc, 1, length, f);

    int lenDic = strlen(dicc);
    int lenInp = strlen(input);

    for (i = 0 ; i < lenInp ; i++)
    {
        for (k = 4 ; k < lenDic ; k = k + 4)
        {
            if (strcmp(input[i],dicc[k]) == 0)
            {
                output[i] = dicc[k+2];
                printf("%c",output[i]);
            }
        }
    }
    fclose(f);
}

I get the below warnings, and the program doesn't work. Anyone who can help me with this strcmp-warning and know how I should rearrange my program in order to meet its requirements?

warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [enabled by default]
if (strcmp(input[i],dicc[k]) == 0)
^
In file included from crypt.c:3:0:
/usr/include/string.h:144:12: note: expected ‘const char *’ but argument is of type ‘char’
 extern int strcmp (const char *__s1, const char *__s2)
            ^
warning: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast [enabled by default]
if (strcmp(input[i],dicc[k]) == 0)
^
In file included from crypt.c:3:0:
/usr/include/string.h:144:12: note: expected ‘const char *’ but argument is of type ‘char’
extern int strcmp (const char *__s1, const char *__s2)
           ^
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Carl
  • 101
  • 1
  • 11

1 Answers1

3

Use strcmp() to compare strings, not characters. For your case this

if (input[i] == dicc[k])

should work.

Note that char is just an integer type not a string type, there is no string type in c. And one more thing, write safe code! Check of fopen() succeeded, and if length doesn't exceed 999. Pretty much every thing that can be considered an error.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • Thanks a million! And thanks for the tips on safe code! – Carl Nov 21 '15 at 22:24
  • In the encryption, I would like it to avoid to translate spaces and that they should be translated as just spaces. The spaces aren't included in the key. When I put an "else if" statement under the if statement, stating: (input[i]=" ") then the output[i]=" ". And it gives me the same error as before. How to I implement this kind of condition? – Carl Nov 21 '15 at 22:33