Skip to main content

Hackerrank > CPP > Strings

                               Strings

01-Strings

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 <span class="MathJaxPreview">ith 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 aand b respectively. In the second line print the string produced by concatenatinga and b (a+b). In the third line print two space-separated strings, a and baand 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
  • a=abcd"
  • b=ef"
  • |a|=4
  • |b|=2
  • a+b=abcdef"
  • a=ebcd"
  • b=af"
Solution
#include <iostream>
#include <string>
using namespace std;
int main() {
    string a, b;
    cin >> a;
    cin >> b;
    cout << a.size() << " " << b.size() << "\n"; // Line 1
    cout << a << b << "\n"; // Line 2
    // Line 3
    if (b.size()) {
        cout << b[0];
    }
    for (int i = 1; i < a.size(); i++) {
        cout << a[i];
    }
    cout << " ";
    if (a.size()) {
        cout << a[0];
    }
    for (int i = 1; i < b.size(); i++) {
        cout << b[i];
    }
    cout << "\n";
    return 0;
}

02-StringStream

stringstream is a stream class to operate on strings. It basically implements input/output operations on memory (string) based streams. stringstream can be helpful in different type of parsing. The following operators/functions are commonly used here
  • Operator >> Extracts formatted data.
  • Operator << Inserts formatted data.
  • Method str() Gets the contents of underlying string device object.
  • Method str(string) Sets the contents of underlying string device object.
Its header file is sstream.
One common use of this class is to parse comma-separated integers from a string (e.g., "23,4,56").
stringstream ss("23,4,56");
char ch;
int a, b, c;
ss >> a >> ch >> b >> ch >> c;  // a = 23, b = 4, c = 56
You have to complete the function vector parseInts(string str)str will be a string consisting of comma-separated integers, and you have to return a vector of int representing the integers.
Note If you want to know how to push elements in a vector, solve the first problem in the STL chapter.
Input Format
The first and only line consists of n integers separated by commas.
Output Format
Print the integers after parsing it.
P.S.: I/O will be automatically handled. You need to complete the function only.
Sample Input
23,4,56
Sample Output
23
4
56
Solution
#include <sstream>
#include <vector>
#include <iostream>
using namespace std;
vector<int> parseInts(string str) {
    stringstream ss(str);
    vector<int> result;
    char ch;
    int currentNumber;
    while(ss >> currentNumber) {
        result.push_back(currentNumber);
        ss >> ch;
    }
    return result;
}
int main() {
    string str;
    cin >> str;
    vector<int> integers = parseInts(str);
    for(int i = 0; i < integers.size(); i++) {
        cout << integers[i] << "\n";
    }
    return 0;
}

Comments

Popular posts from this blog

ORACLE 9i practice solutions

Created by BCL easyConverter SDK 3 (HTML Version)

Zoho Puzzle Questions With Answers

Measuring Time Logic Puzzle You are given with two ropes with variable width. However if we start burning both the ropes, they will burn at exactly same time i.e. an hour. The ropes are non-homogeneous in nature. You are asked to measure 45 minutes by using these two ropes.

How can you do it?

Please note that you can’t break the rope in half as it is being clearly stated that the ropes are non-homogeneous in nature.
Answer & Explanation Solution: 45 minutes

Explanation :
All you have to do is burn the first rope from both the ends and the second rope from one end only simultaneously. The first rope will burn in 30 minutes (half of an hour since we burned from both sides) while the other rope would have burnt half. At this moment, light the second rope from the other end as well. Where, the second rope would have taken half an hour more to burn completely, it will take just 15 minutes as we have lit it from the other end too.

Thus you have successfully calculated 30+15 = 45 minutes …

Hackerrank > SQL > Basic Select

Select
01-Select All
Given a City table, whose fields are described as +-------------+----------+ | Field       | Type     | +-------------+----------+ | ID          | int(11)  | | Name        | char(35) | | CountryCode | char(3)  | | District    | char(20) | | Population  | int(11)  | +-------------+----------+
write a query that will fetch all columns for every row in the table.

My Solution
SELECT*FROM city;
---------------------------------------------------------------------------------
02-Select by ID
Given a City table, whose fields are described as