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");
You cannot define a function inside another one. encryption is defined in main :/
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.
Related
I have been working some time on the code below but for some reason there is no way I can print out correctly the chars array of an array after returning it and using it in the main function.
I cannot come up with any other things I could print in the code to check what is wrong. I do have checked other similar posts in the forum but I cannot spot any differences between the answers given there and how my code is written.
Does anyone spot anything wrong?
Many thanks in advance.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_STRING 35
char **getWordsList(size_t *wordsNumber);
int main(){
int wordsNumber;
char **arrayOfArrays=getWordsList(&wordsNumber);
printf("WordsNumber is %d",wordsNumber);
printf("\nPrinting the resulting array of arrays: \n");
for (size_t indx=0; indx<wordsNumber; indx++){
printf("%s ", *(arrayOfArrays+indx)); //WHY ISNT THE STRING PRINTED CORRECTLY ?
}
free(*arrayOfArrays);
free(arrayOfArrays);
return 0;
}
char **getWordsList(size_t *wordsNumber){
printf("Please enter the number of words: ");
scanf("%zu",wordsNumber);
fflush(stdin);
char **wordsList=malloc(sizeof (char *)*(*wordsNumber));
if (wordsList!=NULL){
for (size_t indx=0; indx<*wordsNumber; indx++)
{
char inputWord [30];
printf("Please enter a word: ");
fgets(inputWord,sizeof(inputWord),stdin);
fflush(stdin);
printf("Word is %s",inputWord);
*(wordsList+indx)=malloc(MAX_STRING*sizeof(char));
if (*(wordsList+indx)){
*(wordsList+indx)=inputWord;
printf("Added array component is: %s\n",*(wordsList+indx));
}
}
return wordsList;}
else{
printf("Error in allocating memory for array of arrays");
}
}
Thanks to the help I got from the post comments, I was able to produce a working code. I am posting it so others who are struggling wih similar problems can benefit from it.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_STRING 35
char **getWordsList(size_t *wordsNumber);
int main(){
int wordsNumber;
char **answer=getWordsList(&wordsNumber);
printf("WordsNumber is %d",wordsNumber);
printf("\nChecking: \n");
for (size_t indx=0; indx<wordsNumber; indx++){
printf("%s ", answer[indx]);
free(answer[indx]);
}
free(answer);
return 0;
}
char **getWordsList(size_t *wordsNumber){
long wordsNumberFoo;
char wordsNumberStr [40];
char *wordsNumberPtr;
printf("Please enter the number of words: ");
fgets(wordsNumberStr, sizeof(wordsNumberStr), stdin);
wordsNumberFoo=strtol(wordsNumberStr, &wordsNumberPtr, 10);
*wordsNumber=(size_t)wordsNumberFoo;
char **wordsList=malloc(sizeof (char *)*(*wordsNumber));
if (wordsList!=NULL){
for (size_t indx=0; indx<*wordsNumber; indx++)
{
char inputWord [30];
printf("Please enter a word: ");
fgets(inputWord,sizeof(inputWord),stdin);
inputWord[strcspn(inputWord, "\n")] = 0;
printf("Word is %s\n",inputWord);
strncpy(*(wordsList+indx),inputWord, MAX_STRING);
printf("Added array component is: %s\n",*(wordsList+indx));
}
return wordsList;}
else{
printf("Error in allocating memory");
return NULL;
}
}
I am still new to programming and there are a lot of things I still don't know but I'd like to ask why my if statement doesn't seem to be working properly. It seems the value
of strcmp(bookName, tolower(searchedName)) when the variable searchedName = "introduction to c" is not 0.
Why is this?
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char bookName[30] = "introduction to c programming";
char searchedName[30];
printf("Enter the book you are searching for: ");
scanf("%s", &searchedName);
if (strcmp(bookName, tolower(searchedName)) != 0) {
printf("The book is not in elibrary");
} else {
printf("The book is in elibrary");
}
return 0;
}
tolower() is for converting characters, not strings. You will have to apply it to each characters in the string separately.
You don't need & before arrays in this case because arrays in expressions are automatically converted to pointers (except for some case).
%s in scanf() will stop at whitespace character. %[^\n] is useful to read until hitting a newline character.
Try this:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char bookName[30] = "introduction to c programming";
char searchedName[30];
char searchedName_lower[30];
int i;
printf("Enter the book you are searching for: ");
scanf("%[^\n]", searchedName);
i = 0;
do {
searchedName_lower[i] = tolower((unsigned char)searchedName[i]);
} while (searchedName[i++] != '\0');
if (strcmp(bookName, searchedName_lower) != 0) {
printf("The book is not in elibrary");
} else {
printf("The book is in elibrary");
}
return 0;
}
I want the string to be printed till character ('e') comes.
Code which I tried:-
#include <stdio.h>
int main() {
int a,i,x;
char b[10];
char ch;
//enter input string
for(i=0;i<10;i++)
scanf("%c",&b[i]);
for(i=0;i<10;i++)
if(b[i]!='e')
printf("%c",b[i]);
return 0;
}
Input:abcdefghij
Actual output:abcdfghij
Desired output:abcd
Question : Where am I wrong ? Will putting a break inside if block work here?
This is much cleaner if you want to use scanf.
#include <stdio.h>
int main()
{
char b[101];
scanf("%100s", b);
printf("%s\n", b);
return(0);
}
Or even better.
#include <stdio.h>
#define MAX_LENGTH 100
int main()
{
char b[MAX_LENGTH+1]; // add 1 for the terminating zero
scanf("%100s", b);
printf("%s\n", b);
return(0);
}
This one uses fgets to read the entire line.
#include <stdio.h>
#define MAX_LENGTH 100
int main()
{
char b[MAX_LENGTH];
fgets(b, MAX_LENGTH, stdin);
printf("%s", b);
return(0);
}
How to print a string till limit?
What code should do is use fgets().
Avoid using scanf(). Is is too easy to use wrong.
#include <stdio.h>
#include <string.h>
int main() {
char b[100];
if (fgets(b, sizeof b, stdin)) {
// If code needs to lop off the potential \n at the end
b[strcspn(b, "\n")] = '\0';
printf("%s\n", b);
}
return 0;
}
Advanced issues include how to handle excessively long input lines and error handling - not shown here.
Here is what you need to do
#include <stdio.h>
int main()
{
int a,i,x;
char b[10];
char ch;
//enter input string
for(i=0;i<10;i++)
{
scanf("%c",&b[i]);
}
for(i=0;i<10;i++)
{
if(b[i]=='e')
{
break;
}
}
return 0;
}
re
There are several mistakes!
If you are initializing your loops from 0 then you need to set the condition till i<100.
Change your format specifiers to %s.
Change your IF statement to if(b[i]!='\0').
#include <stdio.h>
int main()
{
int i;
char b[10];
for(i=0;i<10;i++)
{
scanf("%c",&b[i]);
}
for(i=0;i<10;i++)
{
if(b[i]=='e')
{
break;
}
printf("%c",b[i]);
}
return 0;
}
How do I put isalpha and isdigit in a while(1) loop?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
int i;
char type[256];
printf("You can type a number or a word. Type exit to exit! \n");
printf("Type: ");
fgets (type, 256, stdin);
if (isalpha(type[i]))
{
printf("Typed text: %s\n", type);
if((strcmp(type,"exit\n") == 0))
{
printf("Exiting...\n");
exit(1);
}
}
else if (isdigit(type[i]))
{
printf("Typed number: %s\n", type);
}
else
{
printf("Typed: %s\n", type);
printf("Its not a letter or number...?!\n");
}
}
I tried adding while(1) at the start at the code and close it at the end of code, but as soon as I enter number or letter the console crashes... Could someone please help me with this?
Your problem is not a loop problem, you need to give a value to i , as it is undefined and you get a nice crash. Please replace
int i;
with
int i=0;
Below is my code. I would like to know why within the while-loop the code doesn't ask for another word(s1). The find_anagram function is omitted. How can I loop the program so that it asks for a new word every time the answer is 1?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char s1[20];
int answer;
FILE *fp1;
char leksi[100];
fp1=fopen("C:/Users/inspiron/Desktop/englishWords.txt","r");
answer=1;
while(answer==1){
fgets(s1,20,stdin);
do {
fgets(leksi,20,fp1);
if(find_anagram(leksi,s1)==1){
printf("%s",leksi);
}
} while (!feof(fp1));
memset(leksi, 0, sizeof leksi);
memset(s1, 0, sizeof s1);
printf("Enter another word? yes(1) or no(0)?\n");
scanf("%d",&answer);
}
fclose(fp1);
return 0;
}