Thursday, January 2, 2020

Playing With Characters - Hacker Rank Solution

Objective
This challenge will help you to learn how to take a character, a string and a sentence as input in C.

Playing With Characters - Hacker Rank Solution
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: 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;
}

23 comments:

  1. Replies
    1. #include
      #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;
      }

      Delete
    2. the 's' is not even declared, how are we supposed to declare it as?

      Delete
  2. yes i too written the same but it is showing error

    ReplyDelete
  3. thanku ,...y program bilkul sahi h

    ReplyDelete
    Replies
    1. sahi kaise hai canu explain me
      i too written the same code n..but ...showing error

      Delete
  4. #include

    int 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);

    }

    ReplyDelete
    Replies
    1. but why scanf("\n") is there after taking all the input, what logic is there behind it?

      Delete
    2. #include
      #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

      Delete
  5. it's showing an error for testcase 3

    ReplyDelete
  6. Very helpful information you published
    Bro keep going. It help me in solving a lot bundle of doubts. Thank you .

    ReplyDelete
  7. char ch;
    char 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;

    ReplyDelete
  8. #include
    #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;
    }

    ReplyDelete
  9. Replies
    1. can 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.

      Delete
  10. The 's' was not even declared. How do we declare for 's'? str and char won't work for declaring 's'

    ReplyDelete
  11. char ch;
    char 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);

    ReplyDelete
  12. #include
    #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;
    }

    ReplyDelete
  13. use & (address operator) with &ch and &s and &sen then all got the right answer

    ReplyDelete

Powered by Blogger.