Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8652993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:26:09+00:00 2026-06-12T14:26:09+00:00

what is your best string comparison algorithm? i find O(n) #include <string> bool str_cpmr(char*

  • 0

what is your best string comparison algorithm?

i find O(n)

#include <string>

bool str_cpmr(char* str1, char* str2)
{
    int l1 = strlen(str1), l2 = strlen(str2) ;          

    if(l1 != l2)
        return false;

    for(int i = 0 ; i < l1 ; i++)
        if(str1[i] != str2[i])
            return false ;

    return true ;
}

and i wonder if there is any other / better solution.

also, how to test that accurately?

i propose to compare

  • 100 matches
  • 100 strings differing by one char swap

is there more to test string compare ?

how is it in stl c++ (slt string::compare) ?

thanks!!!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:26:11+00:00Added an answer on June 12, 2026 at 2:26 pm

    You function is O(n), but still takes roughly double the time necessary — strlen walks through the string to find the length, then (assuming they’re the same length) you walk through the strings again comparing the characters.

    Instead of that, I’d walk through the strings until you reach a mismatch or the end of both strings. If you reach a mismatch, you return false. You return true if and only if you reach the end of both strings (simultaneously) without any mismatches first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best practice to include Javascript to your web applications? Consider the
$('.s').keyup(function(e) { if (!/[A-Za-z0-9]/.test(String.fromCharCode(e.which))) { return false; } I wonder what is the best
What's best practice (in VB.Net): Function GetSomething() as String GetSomething = Here's your string
What are your best examples of using Reflection in production code?
What are your best tips for debugging Python? Please don't just list a particular
I know that SQL Server does not have boolean data type and your best
What is the best way of showing your ExtJs app after a page refresh?
What would be best practice to localize your ASP.NET MVC application? I would like
I would like your advice about how best to solve my problem. In a
What is the best way to measure coverage for your Acceptance Tests?? How do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.