Question

Define a function to check if a C-String is symmetric. A string is called symmetric if...

Define a function to check if a C-String is symmetric. A string is called symmetric if the
first half sequence of its characters mirrors the second half. For example, all the strings
“ABCCBA”, “ABCBA”, and “MOM” are symmetric. (In C++)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <cstring>
using namespace std;

bool is_symmetric(char *string) {
    int len = strlen(string), i;
    for (i = 0; i < len / 2; ++i) {
        if (string[i] != string[len-i-1]) {
            return false;
        }
    }
    return true;
}

int main() {
    char str[100];
    cout << "Enter a string: ";
    cin.getline(str, 100);
    if (is_symmetric(str)) {
        cout << str << " is symmetric" << endl;
    } else {
        cout << str << " is not symmetric" << endl;
    }
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Define a function to check if a C-String is symmetric. A string is called symmetric if...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • D.1 [3] Define a function called funD1...) that receives a string and returns a new string...

    D.1 [3] Define a function called funD1...) that receives a string and returns a new string with all the characters in the original string in an EVEN position. As an example, the following code fragment: print (funD1('abcde')) should produce the output: ace D.2 [6] Define a function funD2(...) which receives a list ist that contains single letters, single digits and single special characters and the list contains at least one element of each type). The function returns a string that...

  • Define a function called AddEvenPosDigs(string), which takes a string (with symbols and characters) as an argument,...

    Define a function called AddEvenPosDigs(string), which takes a string (with symbols and characters) as an argument, and returns an integer. This function should add the digits of a string that are in an even position. If there are no digits, the function should return -1. As an example, the following code fragment: string = "a12b056jk"; result=AddEvenPosDigs(string); print(result) should produce the output: 8

  • ****Using c++ (Check Substrings) Write the following function to check whether string s1 is a substring...

    ****Using c++ (Check Substrings) Write the following function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. int indexOf(const string& s1, const string& s2) Write a test program that reads two strings and checks whether the first string is a substring of the second string.

  • C++ Define a generic function called CheckOrder() that checks if four items are in ascending, neither,...

    C++ Define a generic function called CheckOrder() that checks if four items are in ascending, neither, or descending order. The function should return -1 if the items are in ascending order, 0 if the items are unordered, and 1 if the items are in descending order. The program reads four items from input and outputs if the items are ordered. The items can be different types, including integers, strings, characters, or doubles. Ex. If the input is: bat hat mat...

  • Define a function called repeat_middle which receives as parameter one string (with at least one character),...

    Define a function called repeat_middle which receives as parameter one string (with at least one character), and it should return a new string which will have the middle character/s in the string repeated as many times as the length of the input (original) string. Notice that if the original string has an odd number of characters there is only one middle character. If, on the other hand, if the original string has an even number of characters then there will...

  • Write the following program in C Language using standard library functions. /*Implement the Find function, called...

    Write the following program in C Language using standard library functions. /*Implement the Find function, called in the program below. This function receives two strings, and looks for the first occurrence of the second string in the first string, returning the number of characters it is in, relative to the beginning of the first string. If not, returns -1. In the program, a string with two news headlines is given, and the sub-strings "iPad" and "Huawei” are searched for, having...

  • Please use C++ to code Exercise P10.11. Implement a function vector<string> generate_subsets (string s) that generates...

    Please use C++ to code Exercise P10.11. Implement a function vector<string> generate_subsets (string s) that generates all subsets of characters of a string. For example, the subsets of char acters of the string "rum" are the eight strings rum", "ru" Note that the subsets don't have to be substrings-for example, "rm" isn't a sub- string of rum , "rm" ,um

  • using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings separated by a space. The first string will consist of the following sets of characters: +, *, $, and {N} which is optional. The plus (+) character represents a single alphabetic character, the ($) character represents a number between 1-9, and the asterisk (*) represents a sequence of the same character of length 3 unless it is followed by {N} which represents how many...

  • Question / of 2. Case insensitive string compare: [40 marks/ Write a function that compares two...

    Question / of 2. Case insensitive string compare: [40 marks/ Write a function that compares two strings while ignoring the case of alphabetical characters. Your program will take two strings as input from the user and use your function to compare them. Assume a maximum C-string size of 1000 characters. Make sure your code works for any input number, not just the test cases. Your code will be tested on other test cases not listed here. Please properly comment your...

  • using java String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    Have the function wildcard(str) read str which will contain two strings separated by a space.The first string will consist of the following sets of characters: +, *, $ and {N} which is optional.The plus (+) character represents a single alphabetic character, the ($) character represents anumber between 1-9, and asterisk (*) represents a sequence of the same character of length 3unless it is followed by {N} which represents how many characters would appear in thesequence where N will be at...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT