Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
#include <stdio.h>
int main()
{
char c = 'A';
while (c != ',')
{
printf("Input a character:");
scanf("%c", &c);
if (c >= '0' && c <= '9')
{
printf("%d\n", (int)c);
}
}
}
After taking in the first set of input, this code prints out "Input a character" twice each time - why is this?
cause you press a number PLUS enter and enter will be read by scanf() at the next call
#include <stdio.h>
int main(void) {
char c = 'A';
while (c != ',') {
printf("Input a character:");
if (scanf("%c", &c) != 1) {
return 0; // we stop if user don't input anything
}
if (c >= '0' && c <= '9') {
printf("%d\n", (int)c); // by the way did you want (int)(c - '0') ?
} else {
printf("enter a number ! you enter %d\n", c);
}
}
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to create a program that reads a user input strings and prints only those are not ‘a-z’ or ‘A-Z’. The following program can print string of characters. But how do I write a C program that reads a user input strings and prints only those are not ‘a-z’ or ‘A-Z’? I appreciate any help that I can get.
#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
gets(name); // read string
printf("Name: ");
puts(name); // display string
return 0;
}
To start with... Never use gets (nor scanf("%s" …). Use fgets.
Then you just to iterate over the string and check if the individual character is in the range of characters you don't want to print.
#define MAX_LEN 30
int main()
{
char name[30];
printf("Enter name: ");
if (fgets(name, MAX_LEN, stdin) != NULL)
{
int i = 0;
while (name[i])
{
if ((name[i] < 'a' || name[i] > 'z') &&
(name[i] < 'A' || name[i] > 'Z'))
putchar(name[i]);
++i;
}
}
return 0;
}
Input:
12john34BEN56 78Al9
Output:
Enter name: 12john34BEN56 78Al9
123456 789
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm quite new to C and I'm trying to write a loop that takes input with getchar, then print only the U's and K's from the input using putchar.
I tried this:
printf("Enter a bunch of letters: ");
char ch;
while (ch != 'x') {
ch = getchar();
if ( ch >= 'a' && ch <= 'z') {
putchar(ch - 32);
ch;
}
}
Looks to me like you're trying to read input until 'x' is entered, then print the U's and K's from said input. Try this.
Per your comment, seems like you want to print them as upper whether or not they're read as upper. You can use tolower() for that.
char ch;
while ((ch = getchar()) != 'x')
if (toupper(ch) == 'U' || toupper(ch) == 'K')
putchar(toupper(ch));
#include <stdio.h>
int main()
{
puts("(I will print U and K only): ");
int c;
while(EOF != (c=getchar())){
if(c=='U'||c=='K')
putchar(c);
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
*the code is from a book named
Programming with c by Byron gottfried *
*when i try to input a character, the program stops *
int main()
{
char line[80];
int count;
printf(" Enter a line of text below:\n");
scanf("%[^\n], &line");
for(count=0;line[count]!='\0';++count){
if(((line[count]>='0') && (line[count] < '9'))||
((line[count] >= 'A') && (line[count]< 'Z'))||
((line[count]>= 'a' ) && (line[count] <'z' )))
putchar(line[count]+1);
else if(line[count] =='9' ) putchar('0');
else if(line[count] == 'Z')putchar('A');
else if(line[count] == 'z')putchar('a');
else putchar('.');
}
return 0;
}
The line:
scanf("%[^\n], &line");
Should be:
scanf("%[^\n]", line);
i.e. Put the closing " in the format string in the correct place, and no & before line.
Moreover, on some platforms the stdout buffer is not flushed until it contains a newline or becomes full, so you should add a fflush( stdout) call. You will get away with it on Windows.
You might also consider simplifying the code by using the ctype.h functions.
Since this is a simple transcription error (the book referenced does not contain this error) rather than a programming question, and the question has been closed, this purely an aside, but I'd suggest the following implementation.
#include <stdio.h>
#include <ctype.h>
int main()
{
char line[80];
int count;
/* read in the entire string */
printf("Enter a line of text below:\n");
scanf("%[^\n]", line);
/* encode each individual character and display it */
for (count = 0; line[count] != '\0'; ++count)
{
char plaintext = line[count];
char encoded = '.';
if (isupper(plaintext))
{
encoded = (((plaintext + 1) - 'A') % 26) + 'A';
}
else if (islower(plaintext))
{
encoded = (((plaintext + 1) - 'a') % 26) + 'a';
}
else if (isdigit(plaintext))
{
encoded = (((plaintext + 1) - '0') % 10) + '0';
}
putchar(encoded);
}
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to convert from hexadecimal string to decimal number. But my result is different.
This is my code in c:
#include <stdio.h>
main() {
char c[100];
int n,i;
n=0;
printf("Enter Your Hexadecimal:\n");
scanf("%s",c);
for(i=0; c[i]!='\n' ; i++)
if(c[i]>='0' && c[i]<='9') n=n*16+(c[i]-'0');
else if(c[i]=='a' || c[i]=='A') n=n*16+(10);
else if(c[i]=='b' || c[i]=='B') n=n*16+(11);
else if(c[i]=='c' || c[i]=='C') n=n*16+(12);
else if(c[i]=='d' || c[i]=='D') n=n*16+(13);
else if(c[i]=='e' || c[i]=='E') n=n*16+(14);
else if(c[i]=='f' || c[i]=='F') n=n*16+(15);
printf("%d",n)
getch();
}
The result is
Enter Your Hexadecimal:
2f
-585093843 /* This is the result of my code*/<br>
..........Added Answer After Some Times!................
After one year I add the answer of this question for beginners and newbies incC Language ...Hope to be useful
#include "stdio.h"
int main() {
char c[100];
int n,i;
n=0;
printf("Enter Your Hexadecimal:\n");
scanf("%s",&c);
for(i=0; c[i]!='\n' && c[i]!=0 ; i++){
if(c[i]>='0' && c[i]<='9')
n=n*16+(c[i]-'0');
else if(c[i]=='A' || c[i]=='a')
n=n*16+(10);
else if(c[i]=='B' || c[i]=='b')
n=n*16+(11);
else if(c[i]=='C' || c[i]=='c')
n=n*16+(12);
else if(c[i]=='D' || c[i]=='d')
n=n*16+(13);
else if(c[i]=='E' || c[i]=='e')
n=n*16+(14);
else if(c[i]=='F' || c[i]=='f')
n=n*16+(15);
else{
printf("Error:Your number Is Not Valid!");
return -1;
}
}
printf("%d",n);
return 0;
}
Point 1. You never initialized i. No type mentioned for c [maybe it'll get type default to int]
Point 2. You did not use a loop to check all the digits in the input[if it is having more than one digit].
Point 3. You did not check for the success of scanf()
Point 4. You never validated the input, neither you did limit the input to one digit [as per current logic].
Point 5. Did not use a proper signature for main(), no return [more of a good practice point of view]
and maybe many more. I'm out.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a text file in D drive named "data.txt". It has several columns and rows like:
A B A B V B
B V A B A A
B B A A B A
I want to read the "program.txt" file and then convert it to numbers say A=1, B=2, V=3 and then write it so that it will be a array of number. But I'm not sure where to begin. Can someone point me in the right direction?
#include <stdio.h>
int main() {
int n,k;
char C;
FILE *fptr;
fptr=fopen("D:\\program.txt","r+");
printf("Enter n: ");
scanf("%d",&n);
if (n == C){
fprintf(fptr,"%d",3);
}
fclose(fptr);
return 0;
}
From your question, it looks like you want to assign a new number to each character as you encounter it. So you need to keep track of what you've seen and what number was assigned, as well as the current number.
You can do this with an array:
int values[256] = {0};
int next_value = 1;
When you read a character, you can check if you've already seen it. You may also want to check that it's in the right range. If you haven't seen it, assign a number. Otherwise use the number that was originally assigned:
if (c >= 'A' && c <= 'Z') {
if (!values[c]) values[c] = next_value++;
printf ("%d", values[c]);
}
Then it's just a matter of reading the file. There are many ways to do this, but I think the simplest in this case is to read each character from the file in a loop and output them after translating the ones you're interested in:
int c;
while (EOF != (c = getc(fp))) {
if (c >= 'A' && c <= 'Z') {
if (!values[c]) values[c] = next_value++;
printf ("%d", values[c]);
} else {
putchar(c);
}
}
#include <stdio.h>
int main() {
char line_buff[16];
FILE *fptr;
fpos_t pos;
fptr=fopen("D:\\program.txt","r+");
fgetpos(fptr, &pos);
while(fgets(line_buff, sizeof(line_buff), fptr)){//read one line
char *p = line_buff;
while(*p){//convert
if(*p == 'A')
*p++ = '1';
else if(*p == 'B')
*p++ = '2';
else if(*p == 'V')
*p++ = '3';
else
++p;
}
fsetpos(fptr, &pos);
fprintf(fptr, "%s", line_buff);//rewrite
fflush(fptr);
fgetpos(fptr, &pos);//memo position
}
fclose(fptr);
return 0;
}