C program about anagrams doesn't loop - c

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;
}

Related

how to truncate a string array in C

I'm using fgets() to take input for char name[MAX_LENGTH]:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 5;
int main()
{
char name[MAX_LENGTH];
fgets(name, MAX_LENGTH, stdin);
if(strlen(name) >= MAX_LENGTH)
{
name[MAX_LENGTH] = '\0';
}
}
If the user enters "Samuel", I want the program to save the leters "Samue" and ignore the rest. Is there any way of doing this?
Thanks.

In C, how do I give an input to a program previously called with system()?

I currently want to make a program, called with system(), accept an input, in the case where the input is not an argument. As it may be a bit unclear, I am going to illustrate my question with a simple example.
Let's assume we have 2 programs, askname.c and call.c
askname.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char name[10];
printf("What is your name ? ");
fgets(name, 10, stdin);
printf("So you are %s \n", name);
return 0;
}
After this I would call askname with call.c
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]){
char* foo ="test";
system("./askname");
//somehow make askname receive foo to display "So you are test"
return 0;
}
Any idea of how to proceed?
You don't. You use something else. Perhaps popen
FILE *fp = popen("./askname", "w");
fprintf(fp, "%s\n", foo);

Reading a string from stdin in c

I wrote a simple program to read a string.
void main()
{
char *str; /*didn't allocate memory*/
scanf(" %s",str);
printf("%s",str);
}
But it is causing a segmentation fault. Whereas the next one isn't.
void main()
{
char *str;
scanf(" %c",str);
printf("%c\n",str);
}
Would someone mind to clarify how actually this works?
You string isn't allocated. which mean you are writing somewhere you didn't ask for.
however what you can do is:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char c;
while (scanf("%c",&c) && c != '\n')
printf("%c",c);
printf("\n");
return 0;
}
It will read every characters you send in input until you press return.

Print a string till a particular character comes

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;
}

Mono alphabetic cipher doesnt work correctly

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.

Resources