Home >Backend Development >C++ >Here are some potential titles in a question format, based on your provided text: * How to Check if a String Ends with Another String in C ? (Simple and direct) * C : Determining if a String Contai

Here are some potential titles in a question format, based on your provided text: * How to Check if a String Ends with Another String in C ? (Simple and direct) * C : Determining if a String Contai

Susan Sarandon
Susan SarandonOriginal
2024-10-27 16:30:29320browse

Here are some potential titles in a question format, based on your provided text:

* How to Check if a String Ends with Another String in C  ? (Simple and direct)
* C  : Determining if a String Contains a Specific Ending (More descriptive)
* Efficiently C

How to Determine if a String Ends with Another String in C

Determining if one string ends with another is a common programming task. In C , this can be achieved by comparing the last n characters of the strings using the std::string::compare method.

Implementation:

The provided code snippet demonstrates this approach:

<code class="cpp">#include <iostream>

bool hasEnding(std::string const &fullString, std::string const &ending) {
    if (fullString.length() >= ending.length()) {
        return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
    } else {
        return false;
    }
}

int main() {
    std::string test1 = "binary";
    std::string test2 = "unary";
    std::string test3 = "tertiary";
    std::string test4 = "ry";
    std::string ending = "nary";

    std::cout << hasEnding(test1, ending) << std::endl;
    std::cout << hasEnding(test2, ending) << std::endl;
    std::cout << hasEnding(test3, ending) << std::endl;
    std::cout << hasEnding(test4, ending) << std::endl;

    return 0;
}</code>

Explanation:

  • The hasEnding function takes two string arguments: fullString and ending.
  • It checks if the length of fullString is greater than or equal to the length of ending.
  • If true, it compares the last ending.length() characters of fullString with ending using std::string::compare.
  • If the comparison returns 0, it means the strings end with the same characters, and the function returns true.
  • Otherwise, it returns false.

Usage:

In the main function, four test strings are created and a common ending string "nary". The hasEnding function is then called to determine if each test string ends with "nary". The results are printed to the standard output.

You can use this approach to efficiently perform string comparisons and determine if a string contains a specific ending.

The above is the detailed content of Here are some potential titles in a question format, based on your provided text: * How to Check if a String Ends with Another String in C ? (Simple and direct) * C : Determining if a String Contai. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn