Last letter is input + 1 for some reason - c

I'm doing an exercise to print 3 letters taken from the user. The first two letters end up fine, but the last one is always the letter that comes after what the user inputs for some reason (I've tried a few different combinations of inputs and it's always the same result):
#include <stdio.h>
int main(){
char userChar[3];
int i;
for (i = 1; i <= 3; i++){
printf("Enter letter %d\n", i);
scanf(" %c", &userChar[i]);
}
printf("%c %c %c", userChar[1], userChar[2], userChar[3]);
return 0;
}
The input is: abc
The output is: abd
What did I screw up? Thanks for your help!
Edit made recommended changes but still not working

I'll post this as an answer just to clarify the amendments
#include <stdio.h>
int main(){
char userChar[3];
int i;
for (i = 0; i < 3; i++){ // the loop range
printf("Enter letter %d\n", i + 1); // for human use
scanf(" %c", &userChar[i]);
}
printf("%c %c %c", userChar[0], userChar[1], userChar[2]); // the array indexing
return 0;
}
Program session
Enter letter 1
a
Enter letter 2
b
Enter letter 3
c
a b c

Related

Reading a table with only one scanf

This code with C language is supposed to count all characters equal to A as well as number characters in a table .. ~~ I've started by reading the table's characters giving by the user using a for loop and then i've used another for loop to count the number of characters equal to A as well as numbers~~
THE PROBLEM is with SCANF! what should I do to write scanf one time and not twice ???
#include <stdio.h>
#include <stdlib.h>
int main()
{
char T[100] = {0};
int i=0,N=0,b=0,n=0,x=0,j=0,k=0;
printf("give the number of your table's columns \n");
scanf("%d", &N);
if (N > 0 && N <= 100) {
for (i; i < N; i++) {
scanf("%c",T[i]);
printf("give the character of the column number %d /n", i);
scanf("%c",T[i]);
}
for (i = 0; i < N; i++) {
if (T[i] == 'A') b++;
else if (T[i]<='9' && T[i]>='0') n++;
}
printf("the number of characters equal to A is %d\n",b);
printf("The number of numeric characters is %d\n",n);
}
return 0;
}
your code needing only few editing
#include <stdio.h>
#include <stdlib.h>
int main()
{
char T[100]={0};
int i=0,N=0,b=0,n=0,x=0,j=0,k=0;
printf("give the number of your table's columns:\n");
scanf(" %d", &N);
if (N > 0 && N <= 100)
{
for (i=0; i < N; i++) {
//one scanf removed
printf("give the character of the column number %d \n", i);//<--/n to \n
scanf(" %c",&T[i]); //<-- & added to store the value
}
for (i = 0; i < N; i++) {
if (T[i] == 'A') b++;
else if (T[i]<='9' && T[i]>='0') n++;
}
printf("the number of characters equal to A is %d\n",b);
printf("The number of numeric characters is %d\n",n);
}
return 0;
}
while using scanf use & so it can store value while using scanf give a space before the scanf(" %c",&T[i]);
for (i; i < N; i++) {
scanf("%c",T[i]);
printf("give the character of the column number %d /n", i);
scanf("%c",T[i]);
}
here you tried to override T[i] twice without & in scanf removing one scanf and adding & and also for(i=0;i<n;i++) will be better way to go use \n instead of /n
Is this what you were looking to do?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i, A_bin=0, Num_bin=0,length;
char array[100];
printf("Enter a string of numbers and letters: ");
scanf("%s",array); //Storing the Char array in "array"
length=strlen(array); //Getting the length of the Char array
for(i=0;i<=length;i++)
{
if (array[i]=='A') A_bin+=1;
else if (array[i]>= '0' && array[i]<='9') Num_bin+=1;
}
printf("The amount of 'A's in the string is: %d \n" ,A_bin);
printf("The amount of digits in the string is: %d" ,Num_bin);
return 0;
}
Instead of that story I wrote you. Here is what I think you were looking for right. This only looks for capital 'A' although could be adjusted for any characters you wanted by following the scheme. You could always adjust the array size as well for very large inputs. May have errors, play with it and see how it works. Hope the example I provided helped.
Output Example:
Enter a string of numbers and letters: AAJUR874EYRIAA
The amount of 'A's in the string is: 4
The amount of digits in the string is: 3

Words frequency with number of letters (basic loop only)

I Want to write C program to print words length and their frequency by letters number with basic loops techniques. I could get the word length work but I stuck with frequency
(example: Do 2 not 3 judge 5 a 1 book 4 (had solved this))
there are # words with 1 letter
there are # words with 2 letter
etc...
#include <stdio.h>
int main(void) {
char word[30];
int i = 0,b=0,c=0,j=0,d=0;
printf("Please enter a word: ");
for (i = 0; i < 30 ; i++){
scanf("%s", word);
while (word[b]!='\0'){
b++;
}
printf("%s %d ", word, b);
b = 0;
}
return 0;
}
Your question wasn't completely clear. But from what I understood, you also wanted to print the number of times(frequency) a word of length 'l' is inputted by the user. So I will answer that :
You could just store the length of the word in an array that the user inputs. Once all the inputs are read, you can just print the frequency of each word length from the stored array
Refer the following code to understand what I meant :
#include <stdio.h>
int main(void) {
char word[30];
int i = 0,b=0,c=0,j=0,d=0;
int word_length_freq[30]={0}; //an array which will store the frequency of word length(all initialized to 0)
//eg. if word is "hello" it will increase count of word_length_freq[5] by 1
printf("Please enter a word: ");
for (i = 0; i < 3 ; i++){
scanf("%s", word);
while (word[b]!='\0'){
b++;
}
word_length_freq[b]++;
printf("%s %d ", word, b);
b = 0;
}
for(int i=1;i<30;i++){ //This will print the frequency of all words from length 1 to 30
printf("There are %d words of length %d\n",word_length_freq[i],i);
}
return 0;
}
I hope this solves your question !

Having problems with 2D char arrays

So I've got an assignment where my program asks the brand (10 letters), model (10 letters), age (1986 - 2019) and cost (positive real number) of 10 cars and then wants the program to check which car is the oldest and to print out it's brand and model. I don't have a problem with the first part but with the second part.
The code is:
//First part
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define C 10
#define M 11
int main(void)
{
char brand[C][M];
char model[C][M];
int year[C];
float cost[C];
int i, len1, len2, min;
for(i=0; i<C; i++){
printf("Car %d\n", i+1);
do{
printf("Brand: ");
scanf("%s", brand[i]);
len1 = strlen(brand[i]);
} while(len1<0 || len1>10);
do{
printf("Model: ");
scanf("%s", model[i]);
len2 = strlen(model[i]);
} while(len2<0 || len2>10);
do{
printf("Year: ");
scanf("%d", &year[i]);
} while(year[i]<1986 || year[i]>2019);
do{
printf("Cost: ");
scanf("%d", &cost[i]);
} while(cost[i]<=0);
}
//Second part
year[0] = min;
for(i=0; i<10; i++)
if(year[i] < min){
min = year[i];
printf("\nThe oldest car is %s %s\n", brand[i], model[i]);
}
For some reason it either prints out gibberish in the place of brand[i] or if I lose the columns of the if statement prints out all the car brands and their models, where I only want the oldest one.
Aside from scanf not being recommended there are some problems with this code, first when you read the brand and model you do:
do{
printf("Brand: ");
scanf("%s", brand[i]);
len1 = strlen(brand[i]);
} while(len1<0 || len1>10);
The problem here is that you first write the string to brand[i] and then check if it's too long, but you have already written it into the array so if the string is longer than your space you already have a buffer overflow. Limit the size you can read with scanf using scanf("%10s, brand[i]) or better yet use fgets(brand[i], sizeof(brand[i]), stdin).
Next in the second part you use min without initializing it, and you overwrite the content of year[0] with it. You probably wanted something like:
min = 2020; // or a number that will be bigger than all your cars anyway
int older = 0;
i = 0;
for(i=0; i<C; i++){ // Use C here, you have it might as well use it instead of magic numbers
if(year[i] < min){
older = i;
min = year[i];
}
}
printf("\nThe oldest car is %s %s\n", brand[older], model[older]);
but bare in mind that this solution will print multiple cars if they are the oldest ones and have the same year

Why should i use a space before %c while taking input into an array? [duplicate]

This question already has answers here:
what is the purpose of putting a space in scanf like this scanf(" %c",&ch) in place of scanf("%c",&ch)? [duplicate]
(6 answers)
Closed 6 years ago.
I have just written a program to reverse a word. I have first compiled the program as follows:
//This program will reverse a given word
#include <stdio.h>
int main()
{
int letters, i, j;
printf("Enter the number of letters in your word: ");
scanf("%d", &letters);
int word[letters];
printf("Enter %d Letters: ", letters);
for( i = 0; i < letters; i++){
scanf("%c", &word[i]);
}
for( j = i - 1; j >= 0; j-- ){
printf("%c", word[j]);
}
return 0;
}
Then, I inputted 5 to store in letters and the word "rubel" (ignore the inverted comma) to reverse. The expected output was "lebur". But unfortunately i got ebur. then i recompiled the code as below:
//This program will reverse a given word
#include <stdio.h>
int main()
{
int letters, i, j;
printf("Enter the number of letters in your word: ");
scanf("%d", &letters);
int word[letters];
printf("Enter %d Letters: ", letters);
for( i = 0; i < letters; i++){
scanf(" %c", &word[i]);
}
for( j = i - 1; j >= 0; j-- ){
printf("%c", word[j]);
}
return 0;
}
This time i got expected output which is "lebur". Now, my question is what was wrong before that i didn't get expected output and what have i just done by putting a space that this time i got the expected result. Thanks in advance.
In the first case word[0] became \n (you entered 5\n). In the second case the \n was skipped because you told scanf to skip whitespace characters (by ).

How to correctly handle scanf()

I have to take input in the following format
S1 S2 S3
where S1 is a character and S2,S3 are integers
for example
3
A 123 452
D 450 53
B 330 672
(where the '3' represents the number of queries)
Now I've written the following code for it :
while(i<=Q){
scanf("%c %d %d",&ch,&index,&num);
printf("%c %d %d\n",ch,index,num);
i++;
}
However, for the above shown three values I am getting the following output
0 755130840
A 123 452
123 452
with an extra line at the top and that large value (here 755130840) changing every time.
Where am I going wrong?? I even tried scanning the 3 values individually and flushing the input stream before each scan statement. However, It doesn't help either.
Given the two blank lines, I believe the newline ('\n') is being stored in some variable.How do I handle it?
Add a space before %c in scanf. This will allow scanf to skip any number of white spaces before reading ch.
scanf with blank skips white space (including newlines) and reads the next character that is not white space.
Here is a code , this would be working fine.
#include <stdio.h>
int main(void) {
// your code goes here
int i =0;
int Q = 2;
char ch;
int index;
int num;
while(i<=Q){
scanf(" %c %d %d",&ch,&index,&num);
printf("%c %d %d\n",ch,index,num);
i++;
}
return 0;
}
do you want something like this?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, count, numone, numtwo;
char charip;
printf("Enter numbr of elem:\t");
scanf("%d", &num);
if (num < 0)
{
printf("Enter positive value!!!!!\n");
exit (-1);
}
count = 0;
while (count < num)
{
getchar();
scanf ("%c %d %d", &charip, &numone, &numtwo) ;
printf("%c %d %d\n", charip, numone, numtwo);
count++;
}
return 0;
}

Resources