Skip to main content

String practice sample question in C++ for Code Clash Vol. 2

Write a C++ program for find the  Length of the string without using strlen() function.

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


int main() {
    string str;
    int length = 0;
    cout << "Enter a string: " << endl;
    getline(cin, str);


    for (int i = 0; str[i] != '\0'; i++)

    {
        length++;
    }
    // or we can write but que asking without length() or size()                            

    // length = str.length();


    cout << "Length of string: " << length << endl;
    return 0;
}

 

 

Write a program to check the strength of a password as Weak.

#include <iostream>

using namespace std;

int main() {

    string password;

    cout << "Enter a password: ";

    cin >> password;

    int hasUpper = 0, hasLower = 0, hasDigit = 0, hasSpecial = 0;

    for (int i=0; i<password.size(); i++)

       {

        if (password[i] >= 'A' && password[i] <= 'Z')

            hasUpper = 1;

        else if (password[i] >= 'a' && password[i] <= 'z')

            hasLower = 1;

        else if (password[i] >= '0' && password[i] <= '9')

            hasDigit = 1;

        else hasSpecial = 1;

     }

    if (password.length() >= 8 && hasUpper && hasLower && hasDigit && hasSpecial) {

        cout << "Password is Strong" << endl;

    } else {

        cout << "Password is Weak" << endl;

    }

    return 0;

}

Write a program to check the strength of a password as Weak, Average, or Strong based on specific criteria (e.g., length, special characters, and case sensitivity).

Criteria:

  1. Weak: Password is less than 8 characters or lacks any of the following: uppercase, lowercase, digit, or special character.
  2. Average: Password is at least 8 characters and meets at least two of the criteria (uppercase, lowercase, digit, special character).
  3. Strong: Password is at least 8 characters and meets all four criteria.

#include <iostream>

#include <string>

using namespace std;

 

int main() {

    string password;

    cout << "Enter a password: ";

    cin >> password;

 

    bool hasUpper = false, hasLower = false, hasDigit = false, hasSpecial = false;

 

    for (int i = 0; i < password.size(); i++)

      {

        if (password[i] >= 'A' && password[i] <= 'Z')

            hasUpper = true;      

        else if (password[i] >= 'a' && password[i] <= 'z')

            hasLower = true;       

        else if (password[i] >= '0' && password[i] <= '9')

            hasDigit = true;      

        else

            hasSpecial = true;     

    }

 

    // Check for criteria

    if (password.length() >= 8 && hasUpper && hasLower && hasDigit && hasSpecial)

      {

        cout << "Password is Strong" << endl;

    } else

      {

        cout << "Password is Weak. Suggestions:" << endl;

 

        if (password.length() < 8)

            cout << "- Password must be at least 8 characters long." << endl;

        if (!hasUpper)

            cout << "- Include at least one uppercase letter (A-Z)." << endl;

        if (!hasLower)

            cout << "- Include at least one lowercase letter (a-z)." << endl;

        if (!hasDigit)

            cout << "- Include at least one digit (0-9)." << endl;

        if (!hasSpecial)

            cout << "- Include at least one special character (e.g., !, @, #, $, etc.)." << endl;

    }

 

    return 0;

}

 

Write a program to sort any string in increasing or decreasing order based on user input.

#include <iostream>

using namespace std;

void bubbleSort(string &str, bool ascending) {

    int n = str.size();

    for (int i = 0; i < n - 1; i++) {

        for (int j = 0; j < n - i - 1; j++) {

            if ((ascending && str[j] > str[j + 1]) || (!ascending && str[j] < str[j + 1])) {

                char temp = str[j];

                str[j] = str[j + 1];

                str[j + 1] = temp;

            }

        }

    }

}

 

int main() {

    string str;

    char choice;

    cout << "Enter a string: ";

    cin >> str;

    cout << "Choose sorting order (I for Increasing, D for Decreasing): ";

    cin >> choice;

 

    if (choice == 'I' || choice == 'i') {

        bubbleSort(str, true);

        cout << "String in increasing order: " << str << endl;

    }

    else if (choice == 'D' || choice == 'd') {

        bubbleSort(str, false);

        cout << "String in decreasing order: " << str << endl;

    }

    else {

        cout << "Invalid choice. Please enter I or D." << endl;

    }

    return 0;

}

 

Write a program to count spaces, words, and sentences from a string fetched from a file.

#include <iostream>

using namespace std;

int main() {

    string text;

    cout << "Enter a string: ";

    getline(cin, text);

    int spaces = 0, words = 0, sentences = 0;

    for (int i = 0; i < text.size(); i++)

      {

        if (text[i] == ' ')

            spaces++;

        else if (text[i] == '.' || text[i] == '!' || text[i] == '?')

            sentences++;

    }

 

    words = spaces + 1;

    cout << "Spaces: " << spaces << endl;

    cout << "Words: " << words << endl;

    cout << "Sentences: " << sentences << endl;

    return 0;

}

 

Write a program to reverse a given string.

#include <iostream>

using namespace std;

int main() {

    string str, reversed;

    cout << "Enter a string: ";

    cin >> str;

    int n = str.size();

    for (int i = n - 1; i >= 0; i--) {

        reversed += str[i];

    }

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

    return 0;

}

Write a program to check whether a given number or string is a palindrome.

#include <iostream>

using namespace std;

bool isPalindromeString(string str) {

    int start = 0, end = str.size() - 1;

    while (start < end) {

        if (str[start] != str[end]) {

            return false;

        }

        start++;

        end--;

    }

    return true;

}

 

bool isPalindromeNumber(int num) {

    int original = num, reversed = 0;

    while (num > 0) {

        int digit = num % 10;

        reversed = reversed * 10 + digit;

        num /= 10;

    }

    return original == reversed;

}

 

int main() {

    cout << "Enter '1' to check a number or '2' to check a string: ";

    int choice;

    cin >> choice;

 

    if (choice == 1) {

        int num;

        cout << "Enter a number: ";

        cin >> num;

        if (isPalindromeNumber(num)) {

            cout << num << " is a palindrome number." << endl;

        } else {

            cout << num << " is not a palindrome number." << endl;

        }

    } else if (choice == 2) {

        string str;

        cout << "Enter a string: ";

        cin >> str;

 

        if (isPalindromeString(str)) {

            cout << str << " is a palindrome string." << endl;

        } else {

            cout << str << " is not a palindrome string." << endl;

        }

    } else {

        cout << "Invalid choice!" << endl;

    }

 

    return 0;

}


Reverse a String: Input a string and reverse its characters.

#include <iostream>

using namespace std;

 

int main() {

    string str;

    getline(cin,str);

    int n = str.length();

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

        char temp = str[i];

        str[i] = str[n - i - 1];

        str[n - i - 1] = temp;

    }

    cout << str;

    return 0;

}

 

Check for Palindrome: Determine if a string is a palindrome.

 

#include <iostream>

using namespace std;

 

int main() {

    string str;

    cin >> str;

    int n = str.length();

    bool isPalindrome = true;

 

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

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

            isPalindrome = false;

            break;

        }

    }

 

    if (isPalindrome)

        cout << "Palindrome";

    else

        cout << "Not a Palindrome";

 

    return 0;

}

 

 Count Vowels and Consonants: Count the number of vowels and consonants in a string.

#include <iostream>

using namespace std;

 

int main() {

    string str;

    getline(cin,str);

 

    int vowels = 0, consonants = 0;

 

    for (char c : str) {

        char ch = tolower(c);

        if ((ch >= 'a' && ch <= 'z')) {

            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')

                vowels++;

            else

                consonants++;

        }

    }

 

    cout << "Vowels: " << vowels << endl;

    cout << "Consonants: " << consonants << endl;

 

    return 0;

}

 Find String Length: Implement your own function to calculate the length of a string without using strlen().

 

#include <iostream>

using namespace std;

 

int stringLength(string str) {

    int length = 0;

    for (char c : str)

     {

        length++;

    }

    return length;

}

 

int main() {

    string str;

    getline(cin,str);

 

    cout << "Length of the string: " << stringLength(str) << endl;

    return 0;

} 

 


 


Comments

Popular posts from this blog

Flow Charts - What Is a Flow Chart? When to Use a Flowchart? Flowchart Symbols & Components

  Flow Charts     Flow charts are a useful tool in many situations, as we make a process easy to understand at a glance. Using just a few words and some simple symbols, they show clearly what happens at each stage and how this affects other decisions and actions.   What Is a Flow Chart?     In 1921, the Frank and Lillian presented what was only a "graphic-based method" in a presentation titled: “ Process Charts: First Steps in Finding the One Best Way to do Work ”, to members of the American Society of Mechanical Engineers (ASME). When to Use a Flowchart?     Flowchart is a very simple yet powerful tool to improve productivity in both our personal and work life. Here are some ways flowchart can be helpful:   ·      Document a process ·      Present solution to a problem ·      Brainstorm ideas in a meeting ·      Design an operation system ·    ...

Most Asked Pattern Programs in C

  Pattern Programs in C  

C++ Lec-2 Introduction to Object-Oriented Programming (OOP) in C++

  Introduction to Object-Oriented Programming (OOP) in C++ Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to organize software design. It allows for modelling real-world entities and relationships in a program. C++ is an object-oriented programming language that provides features to implement OOP concepts effectively. Key Concepts of OOP   Class and Object o    Class : A blueprint for creating objects. It defines properties and behaviours of objects.   class Car { public:     string brand;     string model;     int year; };   o    Object : An instance of a class.   Car car1; car1.brand = "Mahindra"; car1.model = "THAR"; car1.year = 2024;   Encapsulation o    Encapsulation is the bundling of data and methods that operate on that data within a single unit, or class. It restricts direct acces...