The Site is under maintenance!..Test link

Assignment5_3

String Operations

String Operations

Answer:

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

int main() {
    char str[] = "Hello, World!";
    int length = strlen(str);
    cout << "Length of the string is: " << length << endl;
    return 0;
}
                            

Answer:

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

int main() {
    char str[] = "Programming";
    for (int i = strlen(str) - 1; i >= 0; i--) {
        cout << str[i];
    }
    cout << endl;
    return 0;
}
                            

Answer:

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

int main() {
    char str[] = "Count the words in this string.";
    int count = 1; // At least one word
    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] == ' ')
            count++;
    }
    cout << "Number of words: " << count << endl;
    return 0;
}
                            

Answer:

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

int main() {
    char str1[50] = "Hello, ";
    char str2[] = "World!";
    strcat(str1, str2); // Concatenate str2 to str1
    cout << "Concatenated string: " << str1 << endl;
    return 0;
}
                

Answer:

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

int main() {
    char str1[] = "Hello";
    char str2[] = "Hello";
    if (strcmp(str1, str2) == 0)
        cout << "Strings are equal." << endl;
    else
        cout << "Strings are not equal." << endl;
    return 0;
}
                

Answer:

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

int main() {
    char str[] = "radar";
    int len = strlen(str);
    bool isPalindrome = true;
    for (int i = 0; i < len / 2; i++) {
        if (str[i] != str[len - 1 - i]) {
            isPalindrome = false;
            break;
        }
    }
    if (isPalindrome)
        cout << "The string is a palindrome." << endl;
    else
        cout << "The string is not a palindrome." << endl;
    return 0;
}
                

Answer:

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

int main() {
    char str[] = "Hello, World!";
    char substr[] = "World";
    char* pos = strstr(str, substr);
    if (pos)
        cout << "Substring found at position: " << pos - str << endl;
    else
        cout << "Substring not found." << endl;
    return 0;
}
                

Answer:

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

int main() {
    char str[] = "Hello";
    int len = strlen(str);
    for (int i = 0; i < len / 2; i++) {
        char temp = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = temp;
    }
    cout << "Reversed string: " << str << endl;
    return 0;
}
                

Answer:

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

int main() {
    char str[] = "HeLLo WoRLD";
    for (int i = 0; str[i] != '\0'; i++) {
        str[i] = tolower(str[i]);
    }
    cout << "Lowercase string: " << str << endl;
    return 0;
}
                

Answer:

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

int main() {
    char str[] = "HeLLo WoRLD";
    for (int i = 0; str[i] != '\0'; i++) {
        str[i] = toupper(str[i]);
    }
    cout << "Uppercase string: " << str << endl;
    return 0;
}
                

Answer:

Output:
S@#E @#RTH
                

Sample Output:

Input: "SaVE EArtH"
Output: "S#@E#A@tH"
                        

Answer:

Output:
coOOMMpppRR