As mentioned in the problem statement, we need to input a character, a string without spaces and a sentence and print each of them in a new line.
To input a character, , the statement is:
To input a string, , the statement is:
To input a sentence, , the statement is:
scanf("%c", &ch);
.To input a string, , the statement is:
scanf("%s", s);
.To input a sentence, , the statement is:
scanf("%[^\n]%*c", sen);
.
In the code below, we have declared , as
char s[20]
, here it represents that the string s, can hold a maximum of 20 characters.
We already learnt in the previous exercise, how to print output in C. Look at the code below to understand how to solve this challenge.
Problem Setter's code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char ch;
char s[20], sen[100];
scanf("%c", &ch);
scanf("%s", s);
scanf("\n");
scanf("%[^\n]%*c", sen);
printf("%c\n", ch);
printf("%s\n", s);
printf("%s\n", sen);
return 0;
}
Wrong ,,,..its showing error
ReplyDelete#include
Delete#include
#include
#include
int main() {
char ch;
char s[20], sen[100];
scanf("%c", &ch);
scanf("%s", &s);
scanf("\n");
scanf("%[^\n]%*c", &sen);
printf("%c\n", ch);
printf("%s\n", s);
printf("%s\n", sen);
return 0;
}
the 's' is not even declared, how are we supposed to declare it as?
Deleteyes i too written the same but it is showing error
ReplyDeletethanku ,...y program bilkul sahi h
ReplyDeletesahi kaise hai canu explain me
Deletei too written the same code n..but ...showing error
#include
ReplyDeleteint main()
{
char ch;
char s[50];
char s1[100];
scanf("%c%*c",&ch);
scanf("%s%*c",&s);
scanf("%[^\n]",&s1);
printf("%c\n",ch);
printf("%s\n",s);
printf("%s",s1);
}
but why scanf("\n") is there after taking all the input, what logic is there behind it?
Deletei have same doubt
Delete#include
Delete#include
#include
#include
int main()
{
char ch;
char str[50];
char sen[100];
scanf("%c",&ch);
printf("%c\n",ch);
scanf("%s",str);
printf("%s",str);
scanf("%[\^n]",sen);
printf("%s",sen);
return 0;
}
correct code for this problem
it's showing an error for testcase 3
ReplyDeleteVery helpful information you published
ReplyDeleteBro keep going. It help me in solving a lot bundle of doubts. Thank you .
char ch;
ReplyDeletechar s[100];
char sen[100];
scanf("%c", &ch);
scanf("\n");
scanf("%s", s);
scanf("\n");
scanf("%[^\n]%*c", sen);
printf("%c\n", ch);
printf("%s\n", s) ;
printf("%s", sen);
return 0;
#include
ReplyDelete#include
int main()
{
int a,b;
float f1,f2;
scanf("%d",&a);
scanf("%d",&b);
scanf("%f",&f1);
scanf("%f",&f2);
printf("%d ",(a+b));
printf("%d\n",(a-b));
printf("%.1f ",(f1+f2));
printf("%.1f\n",(f1-f2));
return 0;
}
Thanks it worked!!
ReplyDeletecan you tell how it worked? Because it did not work for me, as the 's' has not been declared here and I don't know how/what to declare it.
DeleteThe 's' was not even declared. How do we declare for 's'? str and char won't work for declaring 's'
ReplyDeletechar ch;
ReplyDeletechar s[MAX_LIMIT];
char sen[MAX_LIMIT];
scanf("%c",&ch);
scanf("%s",&s);
scanf("\n");
scanf("%[^\n]%*c", sen);
printf("%c\n%s\n%s",ch,s,sen);
#include
ReplyDelete#include
#include
#include
int main() {
char ch;
char s[20], sen[100];
scanf("%c", &ch);
scanf("%s", s);
scanf("\n");
scanf("%[^\n]%*c", sen);
printf("%c\n", ch);
printf("%s\n", s);
printf("%s\n", sen);
return 0;
}
Wt is the output??
Deletethanks your ans is correct.
Deleteuse & (address operator) with &ch and &s and &sen then all got the right answer
ReplyDeletePlaying With Characters – Hacker Rank Solution
ReplyDeletehttps://www.codeworld19.com/playing-with-characters-hacker-rank-solution/