Actual question :
Problem Statement Today is Newton School's first class of this year. Nutan, a student at Newton School, has received his first assignment. He will be given a string as input. His task is to print "Gravity'' if the input is "Apple''; otherwise, he will have to print "Space''.
Can you help Nutan in solving his first assignment? Note that the quotation marks are just for clarity. They are not part of the input string, and should not be a part of your output string. Input The input consists of a single line that contains a string S (1 ≤ length of S ≤ 10). The string only consists of lowercase and uppercase letters. Output Print "Gravity'' or "Space'' according to the input.
What I am trying to do: Basically, I am taking a user-defined string and trying to compare it with the hard input string i.e "Apple". If both the string matches then it will print "Gravity" or else it will print "Space"
#include <stdio.h> // header file for Standard Input Output
#include <stdlib.h> // header file for Standard Library
#include<string.h>
int main() {
char str1[10]="Apple";
char str2[20];
int value;
printf("Enter the input ");
scanf("%s", &str2[20]);
value = strcmp(str1, str2);
if(value==0)
printf("Gravity");
else
printf("Space");
return 0;
}
CodePudding user response:
scanf("%s", &str2[20]);
may invoke undefined behavior by out-of-range access. You should:
- Pass the pointer to the first element of the array, not one to the next element of the last element. (most) arrays in expressions are automatically converted to pointes to their first elements.
- Specify the maximum length to read to avoid buffer overrun.
- Check if reading succeeded.
The line should be:
if (scanf("s", str2) != 1) {
puts("read error");
return 1;
}
CodePudding user response:
Some improvements:
- Don't use
"%s"
, use"%<WIDTH>s"
, to avoid buffer-overflow - Instead of using bare
return 0;
, usereturn EXIT_SUCCESS;
, which is defined in the header filestdlib.h
. - always check whether
scanf()
input was successful or not - Use
const char *
instead ofchar str1[10]
- There's no need for
int value;
- SYNTAX ERROR:
&str2[20]
- There's no need for passing the address of
str2
READ MORE
- Initialize
str2
with zeroes - Add
1
more space in yourstr2
for NULL ('\0'
) terminating character
Final Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
const char *str1 = "Apple";
char str2[21] = {};
printf("Enter the input ");
if(scanf(" s", str2) != 1)
{
fprintf(stderr, "bad input\n");
return EXIT_FAILURE;
}
if(strcmp(str1, str2) == 0)
printf("Gravity");
else
printf("Space");
return EXIT_SUCCESS;
}
CodePudding user response:
You are entering a string starting from the memory address after the last element of the array str2
scanf("%s", &str2[20]);
and then trying to compare the character array str1 with the non-initialized array str2
value = strcmp(str1, str2);
Change the call of scanf like
scanf("s", str2);
And the program will be safer if at least the array str2 will be initially initialized
char str2[20] = "";
Also as the array str1 is not changed then instead of the array you could declare a pointer to the string literal like
const char *str1 = "Apple";
And instead of the calls of printf
printf("Gravity");
//..
printf("Space");
it is better to use calls of puts
puts("Gravity");
//..
puts("Space");
Pay attention to that neither declaration from the header <stdlib.h>
is used in your program. So you may remove this include directive
#include <stdlib.h> // header file for Standard Library