How to define only 4 characters input by user in C - c

I want to make program that allow user input only 4 characters. After that, the system should prompt appropriate error message to the users. How look syntax should be?
Here , i am stuck on this,doesnt know how to continue.
#include<stdio.h>
#include<conio.h>
#include <string.h>
int main(int argc,char **argv)
{
int i;
char cTransID[4];
printf("Enter 4 characters of your name: ");
gets(cTransID);
printf("Length of string c=%d \n",strlen(cTransID));
}

If it's a case where the user absolutely must enter 4 characters, then you can try utilizing "getch()" along with a for loop.
#include<stdio.h>
#include<conio.h>
#include <string.h>
int main(int argc,char **argv)
{
int i;
char cTransID[5];
printf("Enter 4 characters of your name: ");
for (i = 0; i <= 3 ;i++)
{
cTransID[i] = getch();
printf("%c",cTransID[i]);
}
printf("\n\n%s",cTransID);
getch();
return 0;
}

Related

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

stuck with spaces and arrays

I want to display a full name, but I can't enter more than two parts of a name. The program stuck when enter a name which has more characters than the number which array has. How can I solve this?
#include <stdio.h>
#include<stdlib.h>
int main(){
char x[25];
printf("Enter your name");
scanf("%s",x);
printf("Your name is %s", x);
return 0;
}
Thank You
I think this can help you. This program doesnt care how many characters, spaces you entered. It only displays first 24 characters and spaces. (1 for string terminator)
#include <stdio.h>
#include <stdlib.h>
int main(){
char x[25];
char *xx=x;
puts("Input Name");
fgets(xx,25,stdin);
puts(xx);
return 0;
}

C program about anagrams doesn't loop

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

C getting input from CMD

How do you get input using prompt? I tried compiling the code bellow into "a.exe" and executing it from CMD like "gcc a.exe 5", but it doesn't output the number like it was supposed to.
#include <stdio.h>
int main(int a)
{
printf("%d\n", a);
return 1;
}
Have I done anything wrong when installing the compiler or am I trying to run it wrong?
Your main() parameters are wrong, you should do it this way:
int main(int argc, char **argv) {
if(argc > 2) {
printf("%s\n", argv[2]);
}
else {
printf("No arguments\n");
}
}
Note that int argc represents the number of parameters and char **argv is an array containing all the parameters, as strings, including "gcc", "a.exe", etc.
In your case, if you run your program this way: gcc a.exe 5, your parameters would be: argc = 3, argv = ["gcc", "a.exe", "5"]
To get input using the prompt, the easiest way would simply be to use a scanf statement. scanf basically waits for, and scans user input, which can then be stored as a variable. For example, a code that would take input for "Give me a number." and then spits back the result would be:
#include <stdio.h>
int main()
{
int num; //Initializes variable
printf("Please give me a number.\n"); //Asks for input
scanf("%d", &num); //scanf is the function, %d reserves the space, and the &*variable* sets the input equal to the variable.
getchar(); //Waits for user to input.
printf("Your number was %d.\n", num); //Spits it back out.
return 0;
}
The output would be:
[PROGRAM BEGINS]
Please give me a number.
>>>5
Your number was 5.
[PROGRAM ENDS]
#include <stdio.h>
int main(int argc, char *argv[])
{
if(argc == 2)
printf("%d\n", atoi(argv[1]));
return 0;
}

Taking continuous input from user in C [duplicate]

This question already has answers here:
How to read unlimited characters in C
(3 answers)
Closed 8 years ago.
I am working on a project where I have to take input from a user in the terminal in c until they input quit then I end the program. I realized I cant do this:
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char **argv){
char *i;
while(1){
scanf("%s", i);
/*do my program here*/
myMethod(i);
}
}
So my question is how can I go about taking this coninuous user input? Can I do it with loops or what else can I do?
First you have to allocate space for the string you are reading, this is normally done with a char array with the size of a macro. char i[BUFFER_SIZE] Then you read data into your buffer,fgets might be better than scanf for that. Finally you check your exit case, a strcmp with "quit".
#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE BUFSIZ /* or some other number */
int main(int argc, const char **argv) {
char i[BUFFER_SIZE];
fgets(i, BUFSIZ, stdin);
while (strcmp(i, "quit\n") != 0) {
myMethod(i);
fgets(i, BUFSIZ, stdin);
}
}
Strings obtained with fgets are gurenteed null terminated
scanf() will return number of elements successfully read I will make use of it like below
#include<stdio.h>
#include<string.h>
int main()
{
int a[20];
int i=0;
printf("Keep entering numbers and when you are done press some character\n");
while((scanf("%d",&a[i])) == 1)
{
printf("%d\n",a[i]);
i++;
}
printf("User has ended giving inputs\n");
return 0;
}
You can use a do while loop:
do
{
// prompts the user
}
while (valueGiven != "quit");
using do-while loop.
char *i = null;
char ch = 'a';/
do{
scanf("%s", &i);
/*do my program here*/
myMethod(i);
printf("Do you want to continues.. y/n");
ch = getchar();
}while(ch != 'q');

Resources