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.
Related
I'm trying to make a program which ask a user for their 16 digit credit card number. I want to store the users input into a long int variable. How do I read the users input without using scanf?
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
int main(void)
{
long cn;
char buf[20];
do
{
printf("card number please: ");
fgets(buf, 19, stdin);
cn = strtol(buf, NULL, 0);
}
while (cn < 1);
printf("%ld\n", cn);
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char string[20];
long int val;
fgets(string, 19, stdin);
val=atol(string); //atoi()/atol() for long int /atoll() any of these fns can be used
printf("%s\n%d", string,val);
return 0;
}
//Here you are converting string to long int later using atol
//Inputting string by fgets()
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;
}
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.
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;
}
I'm trying to change the case of a letter entered by the user and store a lower case and a higher case version of the letter in variables. I've written the code below but it's having issues running. Anyone point out what's causing the problems?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
char CaseChange(character){
int lowerc, higherc;
if(isupper(character)){
lowerc = tolower(character);
printf("%s", lowerc);
}
else{
higherc = character;
printf("%s", higherc);
}
return;
}
int main(void){
char character;
printf("Enter a character: ");
scanf("%c", character);
CaseChange(character);
return 0;
}
There are two problems in your code:
printf("%s", ...) is meant for outputting strings (char* and const char*), not single characters. Use printf("%c", ...)
You forgot to #include <ctype.h>
Side-note: You don't have to check if a character is uppercase with isupper(x). tolower(x) will leave already-lowercase characters intact.