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

C Questions

C Questions
C Questions

Note : All the programs are tested under Turbo C/C++ compilers.
It is assumed that,
Programs run under DOS environment, The underlying machine is an x86 system, Program is compiled using Turbo C/C++ compiler.
The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).
Predict the output or error(s) for the following:

void main()
{
int const * p=5; printf("%d",++(*p));
}
Answer:
Compiler error: Cannot modify a constant value.
Explanation:
p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".
main()
{
char s[ ]="man"; int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
Answer: mmmm
aaaa nnnn
Explanation

Zoho Interview | Set 1 (Advanced Programming Round)

Third Round: (Advanced Programming Round) Here they asked us to create a “Railway reservation system” and gave us 4 modules. The modules were:
    1. Booking
    2. Availability checking
    3. Cancellation
    4. Prepare chart
We were asked to create the modules for representing each data first and to continue with the implementation phase.

 My Solution :

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>#include<iostream.h>#include<time.h>#include<iomanip.h>#include<fstream.h>char f[10]="f";char s[10]="s";int addr,ad,flag,f1,d,m,i,amt;float tamt; class login {public:char id[100];char pass[100];char*password;void getid(){ cout<<"Enter your id:";gets(id); password=getpass("Enter the password:");strcpy(pass,password);}void displayid(){ cout<<"Id:";puts(id); cout<<"Password:";puts(pass);}}; class detail {public:in…

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