WebNest
Team/Naimul Islam/Module-9_Practice_Of_String_Class_Build_In_Function_In_Cpp

Repository

Module-9_Practice_Of_String_Class_Build_In_Function_In_Cpp

View on GitHub ↗
C++0 stars0 forks

README

C++ Problem Solving Solutions


Problem 1: Replace All Occurrences of a String

Problem Statement

You will be given two strings S and X. You need to replace all X from string S with a '#' sign.

Input Format

First line will contain T, the number of test cases.

Next T lines will contain a line with S and X.

Solution

#include<bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
        string s, x;
        cin >> s >> x;

        string result = "";
        int s_len = s.length();
        int x_len = x.length();

        for (int i = 0; i < s_len;) {
            int match = 1;

            if (i + x_len <= s_len) {
                for (int j = 0; j < x_len; j++) {
                    if (s[i + j] != x[j]) {
                        match = 2;
                    }
                }
            } else {
                match = 2;
            }

            if (match == 1) {
                result = result + "#";
                i = i + x_len;
            } else {
                result = result + s[i];
                i = i + 1;
            }
        }

        cout << result << endl;
    }

    return 0;
}

Problem 2: Find Jessica

Problem Statement

Write a program to determine if a given string contains the word "Jessica". If the word is present in the string, the program should output "YES", otherwise it should output "NO".

NOTE: You need to find only "Jessica"; not "jessica" or "JeSsica" or any other form. Words are separated by spaces.

Input Format

Input will contain a string S containing names. There is a space in between two names.

Solution

#include<bits/stdc++.h>
using namespace std;

int main() {
    string s;
    bool find = false;

    while (cin >> s) {
        if (s == "Jessica") {
            find = true;
        }
    }

    if (find == true) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

    return 0;
}

Problem 3: Reverse Student Sections

Problem Statement

You will be given data for N students, where each student will have a name (nm), class (cls), section (s) and student ID (id). The Headmaster decided to change the sections of the students. He wants to reverse their sections.

Your task is reverse their section and print all the students data.

Input Format

First line will contain N.

Next N lines will contain nm, cls, s, and id respectively.

Solution

#include<bits/stdc++.h>
using namespace std;

class Student {
public:
    string name;
    int cls;
    char sec;
    int id;
};

int main() {
    int n;
    cin >> n;

    Student students[100];
    string sections = "";

    for (int i = 0; i < n; i++) {
        cin >> students[i].name >> students[i].cls >> students[i].sec >> students[i].id;
        sections += students[i].sec;
    }

    reverse(sections.begin(), sections.end());

    for (int i = 0; i < n; i++) {
        students[i].sec = sections[i];

        cout << students[i].name << " "
             << students[i].cls << " "
             << students[i].sec << " "
             << students[i].id << endl;
    }

    return 0;
}

Problem 4: Sort Students by Total Marks

Problem Statement

You will be given data for N students, where each student will have a name (nm), class (cls), section (s), student ID (id), math marks (math_marks), and English marks (eng_marks).

Your task is to sort the students data according to the total marks (sum of math_marks and eng_marks) in descending order.

If multiple student have the same total marks then sort them according to the id in ascending order.

Input Format

First line will contain N.

Next N lines will contain nm, cls, s, id, math_marks and eng_marks respectively.

Solution

#include <bits/stdc++.h>
using namespace std;

class student {
public:
    string name;
    int cls;
    char s;
    int id;
    int math_marks;
    int eng_marks;

    int total() {
        return math_marks + eng_marks;
    }
};

bool cmp(student l, student r) {
    return (l.total() == r.total()) ? l.id < r.id : l.total() > r.total();
}

int main() {
    int n;
    cin >> n;

    student a[n];

    for (int i = 0; i < n; i++) {
        cin >> a[i].name >> a[i].cls >> a[i].s
            >> a[i].id >> a[i].math_marks >> a[i].eng_marks;
    }

    sort(a, a + n, cmp);

    for (int i = 0; i < n; i++) {
        cout << a[i].name << " "
             << a[i].cls << " "
             << a[i].s << " "
             << a[i].id << " "
             << a[i].math_marks << " "
             << a[i].eng_marks << endl;
    }

    return 0;
}

Problem 5: Sort Students by English Marks

Problem Statement

You will be given data for N students, where each student will have a name (nm), class (cls), section (s), student ID (id), math marks (math_marks), and English marks (eng_marks).

Your task is to sort the students data according to the eng_marks in descending order.

If multiple student have the same eng_marks then sort them according to the math_marks in descending order.

If multiple student have the same math_marks then sort them according to the id in ascending order as the id will be unique.

Input Format

First line will contain N.

Next N lines will contain nm, cls, s, id, math_marks and eng_marks respectively.

Solution

#include<bits/stdc++.h>
using namespace std;

class student {
public:
    string name;
    int cls;
    char s;
    int id;
    int math_marks;
    int eng_marks;
};

bool cmp(student l, student r) {
    if (l.eng_marks != r.eng_marks) {
        return l.eng_marks > r.eng_marks;
    }

    if (l.math_marks != r.math_marks) {
        return l.math_marks > r.math_marks;
    }

    return l.id < r.id;
}

int main() {
    int n;
    cin >> n;

    student a[n];

    for (int i = 0; i < n; i++) {
        cin >> a[i].name >> a[i].cls >> a[i].s
            >> a[i].id >> a[i].math_marks >> a[i].eng_marks;
    }

    sort(a, a + n, cmp);

    for (int i = 0; i < n; i++) {
        cout << a[i].name << " "
             << a[i].cls << " "
             << a[i].s << " "
             << a[i].id << " "
             << a[i].math_marks << " "
             << a[i].eng_marks << endl;
    }

    return 0;
}
← Back to profile