5 Replies - 271 Views - Last Post: 09 November 2019 - 01:26 AM Rate Topic: -----

#1 mikelul   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-November 19

C - Ceaser Code - **Beginner**

Posted 08 November 2019 - 12:07 PM

Hey,
Try to make a Ceaser Code.

I am actually getting not a real printf() back.
Probably somthing wrong with my call of the function.
Would like to rewrite every single letter in the array and printf it afterwards.

Later on, I would like to change they key everytime. So that it is a little bit harder to encrypt ;)
Any tipps on that maybe aswell?

Any hints?


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

int let_converter(int cipher_str_let);
int numb_converter(int cipher_str_numb);
unsigned int cipher(char* cipher_str, int start_key);

// input of the parameter
int main()
{
	int start_keyy = 3;
	char cipher_stri[100];
	
	printf("Please Insert english letter: ");
	fgets(cipher_stri, sizeof(cipher_stri), stdin);
		
}

unsigned int cipher(char* cipher_str, int start_key)
{
	int i=0;
	int cipherValue;
	char cipher;
	int cipher_let;
	
//for all cha in the array
	while( cipher_str[i] != '\0' && strlen(cipher_str)-1 > i)
	{
		//convert the char number ASCII into my alphabet (a=1, b=2, ... A=27,B=28, ...
		int Nummer_function = let_converter(*cipher_str);
		if (cipher_str[i] % 2 == 0)
		{
			if(Nummer_function >=1 && Nummer_function <=26)//check if Capital Letter or not
			{
				cipherValue = (Nummer_function + start_key) % 26;
			}
		
			if(Nummer_function >=27 && Nummer_function <=52)
			{
				cipherValue = (Nummer_function + start_key) % 26;
				cipherValue += 26;
			}
			if (start_key % 2 != 0) //check if key is dividable by 2.
			{
				else if(Nummer_function >=1 && Nummer_function <=26)
				{
					cipherValue = (Nummer_function - start_key) % 26;
				}
		
				else if(Nummer_function >=27 && Nummer_function <=52)
				{
					cipherValue = (Nummer_function - start_key) % 26;
					cipherValue += 26;
				}

			}
			else if (start_key % 2 == 0) //check if key is dividable by 2.
			{
				else if(Nummer_function >=1 && Nummer_function <=26)
				{
					cipherValue = (Nummer_function + start_key) % 26;
				}
		
				else if(Nummer_function >=27 && Nummer_function <=52)
				{
					cipherValue = (Nummer_function + start_key) % 26;
					cipherValue += 26;
				}

			}
		}
			cipher = numb_converter(cipherValue);
			return cipher_str[i]=cipher;
			i++;
	}
}

int numb_converter(int cipher_str_numb)
{
	int let;
	
	if (cipher_str_numb >= 1 && cipher_str_numb <= 26)
	{
	let = cipher_str_numb + 65 - 1; //A=1;Z=26
	}
	
	else if (cipher_str_numb >= 27 && cipher_str_numb<= 52)
	{
	let = cipher_str_numb + 97 - 27; //a=27;z=52
	}
return let;
}

int let_converter(int cipher_str_let)
{
	int numb;
	
	if (cipher_str_let >= 65 && cipher_str_let <= 90)
	{
	numb = cipher_str_let - 65 + 1; //A=1;Z=26
}
	
	else if (cipher_str_let >= 97 && cipher_str_let <= 122)
	{
	numb = cipher_str_let - 97 + 27; //a=27;z=52
	}
return numb;
}



Is This A Good Question/Topic? 0
  • +

Replies To: C - Ceaser Code - **Beginner**

#2 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,672
  • Joined: 25-December 09

Re: C - Ceaser Code - **Beginner**

Posted 08 November 2019 - 02:07 PM

Quote

Any hints?

Perhaps fix all compiler warnings and errors?

Quote

||=== Build: Debug in chomework (compiler: GCC 8-1) ===|
/main.c||In function ‘main’:|
/main.c|12|warning: unused variable ‘start_keyy’ [-Wunused-variable]|
/main.c||In function ‘cipher’:|
/main.c|28|warning: comparison of integer expressions of different signedness: ‘size_t’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]|
/main.c|46|error: expected ‘}’ before ‘else’|
/main.c|60|error: expected ‘}’ before ‘else’|
/main.c|25|warning: unused variable ‘cipher_let’ [-Wunused-variable]|
/main.c|24|warning: unused variable ‘cipher’ [-Wunused-variable]|
/main.c|73|error: data definition has no type or storage class|
/main.c|73|error: type defaults to ‘int’ in declaration of ‘cipher’ [-Wimplicit-int]|
/main.c|73|error: ‘cipher’ redeclared as different kind of symbol|
/main.c|20|note: previous definition of ‘cipher’ was here|
/main.c|73|error: ‘cipherValue’ undeclared here (not in a function); did you mean ‘cipher’?|
/main.c|74|error: expected identifier or ‘(’ before ‘return’|
/main.c|75|error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘++’ token|
/main.c|76|error: expected identifier or ‘(’ before ‘}’ token|
/main.c|77|error: expected identifier or ‘(’ before ‘}’ token|
||=== Build failed: 10 error(s), 4 warning(s) (0 minute(s), 0 second(s)) ===|

Was This Post Helpful? 1
  • +
  • -

#3 mikelul   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-November 19

Re: C - Ceaser Code - **Beginner**

Posted 08 November 2019 - 02:27 PM

UPPPs, sryy, old version ...

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

int let_converter(int cipher_str_let);
int numb_converter(int cipher_str_numb);
unsigned int cipher(char* cipher_str, int start_key);

// input of the parameter
int main()
{
	int start_keyy = 3;
	char cipher_stri[100];
	
	printf("Please Insert english letter: ");
	fgets(cipher_stri, sizeof(cipher_stri), stdin);
		
}

unsigned int cipher(char* cipher_str, int start_key)
{
	int i=0;
	int cipherValue;
	char cipher;
	int cipher_let;
	
	while( cipher_str[i] != '\0' && strlen(cipher_str)-1 > i)
	{
		
		int Nummer_function = let_converter(*cipher_str);
		
		if (cipher_str[i] % 2 == 0)
		{
			if(Nummer_function >=1 && Nummer_function <=26)
			{
				cipherValue = (Nummer_function + start_key) % 26;
			}
		
			if(Nummer_function >=27 && Nummer_function <=52)
			{
				cipherValue = (Nummer_function + start_key) % 26;
				cipherValue += 26;
			}
		}
			if (cipher_str[i] % 2 != 0)
			{
				if(Nummer_function >=1 && Nummer_function <=26)
				{
					cipherValue = (Nummer_function - start_key) % 26;
				}
		
				if(Nummer_function >=27 && Nummer_function <=52)
				{
					cipherValue = (Nummer_function - start_key) % 26;
					cipherValue += 26;
				}
			}
				
			if (cipher_str[i] % 2 == 0)
			{
				if (Nummer_function >=1 && Nummer_function <=26)
				{
					cipherValue = (Nummer_function + start_key) % 26;
				}
		
				if(Nummer_function >=27 && Nummer_function <=52)
				{
					cipherValue = (Nummer_function + start_key) % 26;
					cipherValue += 26;
				}
			}
		}
			cipher = numb_converter(cipherValue);
			return cipher;
			i++;
	}


int numb_converter(int cipher_str_numb)
{
	int let;
	
	if (cipher_str_numb >= 1 && cipher_str_numb <= 26)
	{
	let = cipher_str_numb + 65 - 1; //A=1;Z=26
	}
	
	else if (cipher_str_numb >= 27 && cipher_str_numb<= 52)
	{
	let = cipher_str_numb + 97 - 27; //a=27;z=52
	}
return let;
}

int let_converter(int cipher_str_let)
{
	int numb;
	
	if (cipher_str_let >= 65 && cipher_str_let <= 90)
	{
	numb = cipher_str_let - 65 + 1; //A=1;Z=26
}
	
	else if (cipher_str_let >= 97 && cipher_str_let <= 122)
	{
	numb = cipher_str_let - 97 + 27; //a=27;z=52
	}
return numb;
}




This post has been edited by Skydiver: 08 November 2019 - 05:06 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,672
  • Joined: 25-December 09

Re: C - Ceaser Code - **Beginner**

Posted 08 November 2019 - 03:49 PM

Okay, now it seems that you first need to increase your compiler error level (check your compiler documentation) and fix all warnings.

Quote

||=== Build: Debug in chomework (compiler: GCC 8-1) ===|
main.c||In function ‘main’:|
main.c|12|warning: unused variable ‘start_keyy’ [-Wunused-variable]|
main.c||In function ‘cipher’:|
main.c|27|warning: comparison of integer expressions of different signedness: ‘size_t’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]|
main.c|25|warning: unused variable ‘cipher_let’ [-Wunused-variable]|
main.c|97|warning: ‘numb’ is used uninitialized in this function [-Wuninitialized]|
main.c|74|warning: ‘let’ may be used uninitialized in this function [-Wmaybe-uninitialized]|
main.c|90|warning: ‘cipherValue’ may be used uninitialized in this function [-Wmaybe-uninitialized]|
main.c|23|note: ‘cipherValue’ was declared here|
main.c||In function ‘numb_converter’:|
main.c|92|warning: ‘let’ may be used uninitialized in this function [-Wmaybe-uninitialized]|
main.c||In function ‘let_converter’:|
main.c|108|warning: ‘numb’ may be used uninitialized in this function [-Wmaybe-uninitialized]|
||=== Build finished: 0 error(s), 8 warning(s) (0 minute(s), 0 second(s)) ===|


Haven't yet tried to run the program, I usually fix all warnings and errors first, but one of the issues I see is that there may be "sneak" paths in your functions because of the way the if() statements are formatted.

int numb_converter(int cipher_str_numb)
{
	int let;
	
	if (cipher_str_numb >= 1 && cipher_str_numb <= 26)
	{
	let = cipher_str_numb + 65 - 1; //A=1;Z=26
	}
	
	else if (cipher_str_numb >= 27 && cipher_str_numb<= 52)
	{
	let = cipher_str_numb + 97 - 27; //a=27;z=52
	}
return let;  // let may be returned before it is assigned value.
}



When dealing with if()/else if() statements it is usually best to end the chain with a simple else.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 mikelul   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-November 19

Re: C - Ceaser Code - **Beginner**

Posted 08 November 2019 - 07:40 PM

How do I actually increase my compiler error level?...
Probably there are some issues inside here aswell, but this program works.
Any suggestions what i could make more efficient?

#include <stdio.h>

int caesar (char cipher[], int shift);
int numb_converter(int cipher_str_numb);
int let_converter(int cipher_str_let);
int new_Key(int new_key, int number, int key,int old_key);

int main () 
{
char cipher[101];
int shift;

printf("Enter text to be encrypted: ");
scanf("%s", cipher);

printf("First Shift?: ");
scanf("%d", &shift);

caesar (cipher, shift);

return 0;
}

int caesar (char cipher[], int shift) 
{
	int i = 0;
	int numb;
	int let;
	int key = shift;
	int cypherValue;
	int tmp;
	int key_value;


	while (cipher[i] != '\0') 
	{
	tmp = shift;
	numb=let_converter(cipher[i]);
    if (numb >= 104 && numb <= 130 && tmp%2 == 0)
    {
      numb = (numb + shift)%26 +104;
      key_value = numb;
     } 
     
     else if (numb >= 52 && numb <= 78 && tmp%2==0)
     {
      numb = (numb + shift)%26 + 52; 
      key_value = numb - 52;
     }
     else if (numb >= 104 && numb <= 130 && tmp%2 != 0)
    {
      numb = (numb - shift)%26 +104; //% != Modulu!!! negitve Numbers! multipled 26 and inserted it.
      key_value = numb;
     } 
     else if (numb >= 52 && numb <= 78 && tmp%2 != 0)
    {
      numb = (numb - shift)%26 + 52;
      key_value=numb -52;
     } 
     let = numb_converter(numb);
     int old_key = shift;
     shift = new_Key(tmp,key_value,key,old_key);
    i++;
    printf("%c", let);
   }
  printf("\n");
}

int new_Key(int new_key, int number, int key,int old_key)
{
if (new_key == old_key && old_key == 0)
    {
      new_key = key;
    }
     else if (key == 0)
     {
	new_key = old_key;
	 }
     else
      {
        new_key = number % new_key;
      }
      return new_key;
}

int numb_converter(int cipher_str_numb)
{
	int let;
	
	if (cipher_str_numb >= 104 && cipher_str_numb <= 130)
	{
	let = cipher_str_numb + 65 -104; //A=104;Z=130 
	}
	
	else if (cipher_str_numb >= 52 && cipher_str_numb<= 78)
	{
	let = cipher_str_numb + 97 - 52; //a=52;z=78
	}
return let;
}

int let_converter(int cipher_str_let)
{
	int numb;
	
	if (cipher_str_let >= 65 && cipher_str_let <= 90)
	{
	numb = cipher_str_let - 65 + 104;//A=104;Z=130
}
	
	else if (cipher_str_let >= 97 && cipher_str_let <= 123)
	{
	numb = cipher_str_let - 97 + 52; //a=52;z=78
	}
return numb;
}



This post has been edited by Skydiver: 09 November 2019 - 11:45 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,672
  • Joined: 25-December 09

Re: C - Ceaser Code - **Beginner**

Posted 09 November 2019 - 01:26 AM

Quote

How do I actually increase my compiler error level?...

You find and read the documentation for your compiler, then make the required changes.

Quote

Any suggestions what i could make more efficient?

I already made some suggestions which you seem to have ignored.

You should also find and consistently use some "sane" indentation style, your code as presented is very difficult to read.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1