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);
}
}
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 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);
}
}
}
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
So i recently started c language with no prior knowledge of coding or computer science. Wrote this piece of code to find value of a word using scrabble points as below:
1:AEILNORSTU 2:DG 3:BCMP 4:FHVWY 5:K 8:JX 10:QZ.
# include <stdio.h>
# include <ctype.h>
# include <conio.h>
int main (void)
{
int n=0;
char ch;
clrscr();
printf("Enter the SCRABBLE word\n");
ch = getchar();
while(ch!='\n')
{
toupper(ch);
if(ch =='A'||'E'||'I'||'L'||'N'||'O'||'R'||'S'||'T'||'U')
n=n+1;
else if (ch =='D'||'G')
n=n+2;
else if (ch =='B'||'C'||'M'||'P')
n=n+3;
else if (ch =='F'||'H'||'V'||'W'||'Y')
n=n+4;
else if (ch =='K')
n=n+5;
else if (ch =='J'||'X')
n=n+8;
else if (ch =='Q'||'Z')
n=n+10;
ch = getchar();
}
printf("The value is %d",n);
return 0;
}
So what happens when i run this code is that :
Enter the SCRABBLE word
eg: barrier
The value is 7
though it should be 9 as b carries 3 points as noted above the code,a carries 1,r carriers 1,again r 1 point,i carries 1 point and the last two alphabet are one point each so thats 3+1+1+1+1+1+1=9
An expression like ch =='D'||'G' is equal to (ch == 'D')||'G'.
In other words you first perform the sub-expression ch == 'D'. Then you do a logical or using 'G'. The result will always be true since 'G' is non-zero, and everything non-zero is true in C.
You want ch == 'D' || ch == 'G' instead, to check if ch is equal to 'D' or if ch is equal to 'G'.
This is very basic and every good beginners book would have told you so.
In the specific case of the code you show, the very first check will always be true because of this, and you will not check any other cases.
you forgot to get the return value of toupper. Also you don't check it
(according to man 3 toupper, the function will return the same char in case of failure)
Then your conditions aren't good:
if (ch == 'Q' || 'Z') is different from if (ch == 'Q' || ch == 'Z')
The || means if the right or the left condition is true and in the first case (ch == 'Q') is not always true, but 'Z' is always true.
This means your first condition :
if(ch =='A'||'E'||'I'||'L'||'N'||'O'||'R'||'S'||'T'||'U')
is always true
Here are littles corrections you can apply, this might work
char tmp;
ch = getchar();
while(ch!='\n')
{
//Stocking in a tmp char to check return value
tmp = ch;
//Here
ch = toupper(ch);
if (ch == tmp)
{
//Error
break;
}
{...}
else if (ch =='Q'|| ch =='Z')
n += 10;
ch = getchar();
}
Edit : Correction of return value of toupper();
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
This is from K&R, it's supposed to count digits, white space, etc. I compile fine but when I run I just get a blank screen and all I can do is type inputs. Is this not working because the C language is outdated?
#include <stdio.h>
main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;
printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);
}
Well to put it simply. This loop will end once you type ctrl + d. If you want for it to end when you press enter, just modify it a bit: change
while ((c = getchar()) != EOF)
to
while ((c = getchar()) != '\n')
The program is written to get input until an end-of-file character. Since you're using DOS, enter Ctrl+Z to terminate input (you have to hit "enter" after the Ctrl+Z because keyboard input will be line-buffered).
On edit: Ctrl+Z is the end-of-file (EOF) character in the Windows/DOS world. In UNIX/Linux environments, it is (usually) Ctrl+D.
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;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The following code reads and finds the digit 1. My question is how could it be replaced with a symbol (say 'a') and printed back with the replaced ones.
int i, newtxt;
char text[100];
printf("Enter text: ");
gets(text);
for(i = 0; i<strlen(text); i++)
{
if(text[i] == '1')
replace with a?
}
printf("%s", newtxt);
getch();
return 0;
}
Try this
if(text[i] == '1')
text[i] = 'a';
And do not use gets. Its unsafe. Use fgets instead.
fgets(text, 100, stdin);
Also
printf("%s", newtxt);
is wrong. You have to fix this (try it your self).
Just replace it like this:
if(text[i] == '1')
text[i] = 'a';