Software Development Exam  >  Software Development Notes  >  Basics of C++  >  Assignment: String

Assignment: String | Basics of C++ - Software Development PDF Download

Multiple Choice Questions (MCQs)

Q.1. Which header file should be included to work with strings in C++?
(a) <string>
(b) <iostream>
(c) <cstring>
(d) <fstream>

Ans. (a)

Q.2. What is the correct way to concatenate two strings in C++?
(a) string result = str1 + str2;
(b) string result = str1.concat(str2);
(c) string result = strcat(str1, str2);
(d) string result = str1.append(str2);

Ans. (a)

Q.3. Which function is used to compare two strings in C++?
(a) strcompare()
(b) strcmp()
(c) stringcompare()
(d) stringcmp()

Ans. (b)

Q.4. What is the maximum length of a string in C++?
(a) 256 characters
(b) 512 characters
(c) 1024 characters
(d) There is no fixed maximum length

Ans. (d)

Q.5. Which of the following methods can be used to find the length of a string in C++?
(a) length()
(b) size()
(c) strlen()
(d) All of the above

Ans. (d)

High Order Thinking Questions (HOTS)

Q.1. Write a C++ function to reverse a given string without using any library functions.

#include <iostream>

using namespace std;


void reverseString(string& str) {

    int n = str.length();

    for (int i = 0; i < n / 2; i++) {

        swap(str[i], str[n - i - 1]);

    }

}


int main() {

    string str;

    cout << "Enter a string: ";

    getline(cin, str);


    reverseString(str);


    cout << "Reversed string: " << str << endl;


    return 0;

}

Q.2. Write a C++ program to count the number of occurrences of a specific character in a given string.

#include <iostream>

using namespace std;


int countOccurrences(string str, char ch) {

    int count = 0;

    for (char c : str) {

        if (c == ch) {

            count++;

        }

    }

    return count;

}


int main() {

    string str;

    char ch;


    cout << "Enter a string: ";

    getline(cin, str);


    cout << "Enter a character to count: ";

    cin >> ch;


    int occurrences = countOccurrences(str, ch);


    cout << "Number of occurrences: " << occurrences << endl;


    return 0;

}

Q.3. Write a C++ function to check if a given string is a palindrome or not.

#include <iostream>

using namespace std;


bool isPalindrome(string str) {

    int n = str.length();

    for (int i = 0; i < n / 2; i++) {

        if (str[i] != str[n - i - 1]) {

            return false;

        }

    }

    return true;

}


int main() {

    string str;

    cout << "Enter a string: ";

    getline(cin, str);


    if (isPalindrome(str)) {

        cout << "Palindrome" << endl;

    } else {

        cout << "Not a palindrome" << endl;

    }


    return 0;

}

Q.4. Write a C++ program to find the largest word in a given sentence.

#include <iostream>

using namespace std;


string findLargestWord(string sentence) {

    string largestWord = "";

    string word = "";

    for (char c : sentence) {

        if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\0') {

            if (word.length() > largestWord.length()) {

                largestWord = word;

            }

            word = "";

        } else {

            word += c;

        }

    }

    if (word.length() > largestWord.length()) {

        largestWord = word;

    }

    return largestWord;

}


int main() {

    string sentence;

    cout << "Enter a sentence: ";

    getline(cin, sentence);


    string largestWord = findLargestWord(sentence);


    cout << "Largest word: " << largestWord << endl;


    return 0;

}

Q.5. Write a C++ function to remove all the vowels from a given string.

#include <iostream>

#include <cctype>

using namespace std;


string capitalizeWords(string sentence) {

    string result = "";

    bool capitalizeNext = true;

    for (char c : sentence) {

        if (isspace(c)) {

            capitalizeNext = true;

        } else if (capitalizeNext) {

            result += toupper(c);

            capitalizeNext = false;

        } else {

            result += c;

        }

    }

    return result;

}


int main() {

    string sentence;

    cout << "Enter a sentence: ";

    getline(cin, sentence);


    string capitalized = capitalizeWords(sentence);


    cout << "Capitalized sentence: " << capitalized << endl;


    return 0;

}

Fill in the Blanks

Q.1. The C++ standard library provides the class _________ for working with strings.

Ans. string

Q.2. The function _________ can be used to extract a substring from a given string.

Ans. substr

Q.3. The _________ function can be used to find the first occurrence of a substring within a string.

Ans. find

Q.4. The _________ function is used to convert a string to uppercase.

Ans. toupper

Q.5. The _________ function is used to insert one string into another at a specified position.

Ans. insert

True or False

Q.1. In C++, strings are mutable, meaning their contents can be changed. 

Ans. True

Q.2. The cin function in C++ can be used to read a string with spaces. 

Ans. True

Q.3. The getline function in C++ can be used to read a line of text from the standard input. 

Ans. True

Q.4. The substr function in C++ returns a new string that is a substring of a given string. 

Ans. True

Q.5. The find function in C++ returns the index of the first occurrence of a substring within a string.

Ans. True

Hands-On Questions

Q.1. Write a C++ program to concatenate two strings entered by the user and display the result.

#include <iostream>

using namespace std;


int main() {

    string str1, str2, result;


    cout << "Enter first string: ";

    getline(cin, str1);


    cout << "Enter second string: ";

    getline(cin, str2);


    result = str1 + str2;


    cout << "Concatenated string: " << result << endl;


    return 0;

}

Q.2. Write a C++ program to count the number of vowels in a given string.

#include <iostream>

using namespace std;


int countVowels(string str) {

    int count = 0;

    string vowels = "aeiouAEIOU";

    for (char c : str) {

        if (vowels.find(c) != string::npos) {

            count++;

        }

    }

    return count;

}


int main() {

    string str;

    cout << "Enter a string: ";

    getline(cin, str);


    int vowelCount = countVowels(str);


    cout << "Number of vowels: " << vowelCount << endl;


    return 0;

}

Q.3. Write a C++ program to check if a given string is an anagram or not.

#include <iostream>

using namespace std;


bool isAnagram(string str1, string str2) {

    int count[256] = {0};


    if (str1.length() != str2.length()) {

        return false;

    }


    for (char c : str1) {

        count[c]++;

    }


    for (char c : str2) {

        count[c]--;

    }


    for (int i = 0; i < 256; i++) {

        if (count[i] != 0) {

            return false;

        }

    }


    return true;

}


int main() {

    string str1, str2;

    cout << "Enter first string: ";

    getline(cin, str1);


    cout << "Enter second string: ";

    getline(cin, str2);


    if (isAnagram(str1, str2)) {

        cout << "Anagram" << endl;

    } else {

        cout << "Not an anagram" << endl;

    }


    return 0;

}

Q.4. Write a C++ program to remove all the punctuation marks from a given string.

#include <iostream>

#include <cctype>

using namespace std;


string removePunctuation(string str) {

    string result = "";

    for (char c : str) {

        if (!ispunct(c)) {

            result += c;

        }

    }

    return result;

}


int main() {

    string str;

    cout << "Enter a string: ";

    getline(cin, str);


    string withoutPunctuation = removePunctuation(str);


    cout << "String without punctuation: " << withoutPunctuation << endl;


    return 0;

}

Q.5. Write a C++ program to capitalize the first letter of each word in a given sentence.

#include <iostream>

#include <cctype>

using namespace std;


string capitalizeFirstLetter(string sentence) {

    string result = "";

    bool capitalizeNext = true;

    for (char c : sentence) {

        if (isspace(c)) {

            capitalizeNext = true;

            result += c;

        } else if (capitalizeNext) {

            result += toupper(c);

            capitalizeNext = false;

        } else {

            result += c;

        }

    }

    return result;

}


int main() {

    string sentence;

    cout << "Enter a sentence: ";

    getline(cin, sentence);


    string capitalized = capitalizeFirstLetter(sentence);


    cout << "Capitalized sentence: " << capitalized << endl;


    return 0;

}

The document Assignment: String | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development
70 videos|45 docs|15 tests

Top Courses for Software Development

70 videos|45 docs|15 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Objective type Questions

,

MCQs

,

Free

,

pdf

,

video lectures

,

Summary

,

Semester Notes

,

Important questions

,

ppt

,

practice quizzes

,

Sample Paper

,

mock tests for examination

,

past year papers

,

Assignment: String | Basics of C++ - Software Development

,

Assignment: String | Basics of C++ - Software Development

,

Viva Questions

,

shortcuts and tricks

,

Previous Year Questions with Solutions

,

Extra Questions

,

Exam

,

study material

,

Assignment: String | Basics of C++ - Software Development

;