SORU
17 Mayıs 2009, Pazar


Eğer C dize başka bir dize endswith bulursanız

Nasıl bir dize C başka bir dize ile bitiyorsa bulabilirim ?

CEVAP
17 Mayıs 2009, Pazar


Sadece son karşılaştırınnkarakterler std::string::compare kullanarak:

#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;
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Anthony Cumia

    Anthony Cumi

    5 EYLÜL 2006
  • bombjack2991

    bombjack2991

    29 HAZİRAN 2008
  • Kenneth Håkonsen

    Kenneth Håk

    13 Mart 2011