Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, ...

Strings in C++ – Solution in Hacker Rank - hackerranksolutions8

 

Problem

C++ provides a nice alternative data type to manipulate strings, and the data type is conveniently called string. Some of its widely used features are the following:
Declaration:

string a = "abc";

Size:

int len = a.size();

Concatenate two strings:

string a = "abc";
string b = "def";
string c = a + b; // c = "abcdef".

Accessing i th element:

string s = "abc";
char   c0 = s[0];   // c0 = 'a'
char   c1 = s[1];   // c1 = 'b'
char   c2 = s[2];   // c2 = 'c'

s[0] = 'z';         // s = "zbc"

P.S.: We will use cin/cout to read/write a string.


Input Format

You are given two strings, a and b, separated by a new line. Each string will consist of lower case Latin characters (‘a’-‘z’).

Output Format

In the first line print two space-separated integers, representing the length of a and b, respectively.In the second line print the string produced by concatenating a and b(a+b).In the third line print two strings separated by a space, a’ and b’.a’ and b’ are the same as a and b, respectively, except that their first characters are swapped.


Sample Input :

abcd
ef

Sample Output :

4 2
abcdef
ebcd af

Explanation :

  •  = “abcd”
  •  = “ef”
  • |a| = 4
  • |b| = 2
  • a+b = “abcdef”
  • a’ = “ebcd”
  • b’ = “af”

Solution :- 

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string str1,str2,str3;
    char b,a;
    int strlen1,strlen2;
    //string one 
    cin>>str1;
    //string two
      cin>>str2;
    
    //first string size
    strlen1 = str1.size();
    //second string size
    strlen2 = str2.size();
    cout<<strlen1;
    cout<<" "<<strlen2;
    
    // concatinate two string
    str3 = str1+str2;
    cout<<"\n"<<str3;
    
    // a' b'
    a = str2[0];
    b = str1[0];        
    str1[0] = a;
    str2[0] = b;
    cout<<"\n";
    cout<<str1;
    cout<<" "<<str2;
    return 0;
}

Comments