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

  • Home
  • SEARCH
  • 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 67181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:13:11+00:00 2026-05-10T19:13:11+00:00

I’m trying to diff two strings to determine whether or not they solely vary

  • 0

I’m trying to diff two strings to determine whether or not they solely vary in one numerical subset of the string structure; for example,

varies_in_single_number_field('foo7bar', 'foo123bar') # Returns True, because 7 != 123, and there's only one varying # number region between the two strings. 

In Python I can use the difflib to accomplish this:

import difflib, doctest  def varies_in_single_number_field(str1, str2):     '''     A typical use case is as follows:         >>> varies_in_single_number_field('foo7bar00', 'foo123bar00')         True      Numerical variation in two dimensions is no good:         >>> varies_in_single_number_field('foo7bar00', 'foo123bar01')         False      Varying in a nonexistent field is okay:         >>> varies_in_single_number_field('foobar00', 'foo123bar00')         True      Identical strings don't *vary* in any number field:         >>> varies_in_single_number_field('foobar00', 'foobar00')         False     '''     in_differing_substring = False     passed_differing_substring = False # There should be only one.     differ = difflib.Differ()     for letter_diff in differ.compare(str1, str2):         letter = letter_diff[2:]         if letter_diff.startswith(('-', '+')):             if passed_differing_substring: # Already saw a varying field.                 return False             in_differing_substring = True             if not letter.isdigit(): return False # Non-digit diff character.         elif in_differing_substring: # Diff character not found - end of diff.             in_differing_substring = False             passed_differing_substring = True     return passed_differing_substring # No variation if no diff was passed.  if __name__ == '__main__': doctest.testmod() 

But I have no idea how to find something like difflib for C++. Alternative approaches welcome. 🙂

  • 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. 2026-05-10T19:13:11+00:00Added an answer on May 10, 2026 at 7:13 pm

    This might work, it at least passes your demonstration test: EDIT: I’ve made some modifications to deal with some string indexing issues. I believe it should be good now.

    #include <iostream> #include <string> #include <vector> #include <algorithm> #include <cctype>  bool starts_with(const std::string &s1, const std::string &s2) {     return (s1.length() <= s2.length()) && (s2.substr(0, s1.length()) == s1); }  bool ends_with(const std::string &s1, const std::string &s2) {     return (s1.length() <= s2.length()) && (s2.substr(s2.length() - s1.length()) == s1); }  bool is_numeric(const std::string &s) {     for(std::string::const_iterator it = s.begin(); it != s.end(); ++it) {         if(!std::isdigit(*it)) {                 return false;         }     }     return true; }  bool varies_in_single_number_field(std::string s1, std::string s2) {      size_t index1 = 0;     size_t index2 = s1.length() - 1;      if(s1 == s2) {         return false;     }      if((s1.empty() && is_numeric(s2)) || (s2.empty() && is_numeric(s1))) {         return true;     }      if(s1.length() < s2.length()) {         s1.swap(s2);     }      while(index1 < s1.length() && starts_with(s1.substr(0, index1), s2)) { index1++; }     while(ends_with(s1.substr(index2), s2)) { index2--; }      return is_numeric(s1.substr(index1 - 1, (index2 + 1) - (index1 - 1)));  }  int main() {     std::cout << std::boolalpha << varies_in_single_number_field('foo7bar00', 'foo123bar00') << std::endl;     std::cout << std::boolalpha << varies_in_single_number_field('foo7bar00', 'foo123bar01') << std::endl;     std::cout << std::boolalpha << varies_in_single_number_field('foobar00', 'foo123bar00') << std::endl;     std::cout << std::boolalpha << varies_in_single_number_field('foobar00', 'foobar00') << std::endl;     std::cout << std::boolalpha << varies_in_single_number_field('7aaa', 'aaa') << std::endl;     std::cout << std::boolalpha << varies_in_single_number_field('aaa7', 'aaa') << std::endl;     std::cout << std::boolalpha << varies_in_single_number_field('aaa', '7aaa') << std::endl;     std::cout << std::boolalpha << varies_in_single_number_field('aaa', 'aaa7') << std::endl; } 

    Basically, it looks for a string which has 3 parts, string2 begins with part1, string2 ends with part3 and part2 is only digits.

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

Sidebar

Ask A Question

Stats

  • Questions 112k
  • Answers 112k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Maybe you don't even need a timer for that. Just… May 11, 2026 at 9:56 pm
  • Editorial Team
    Editorial Team added an answer You need to include the header file in your application… May 11, 2026 at 9:56 pm
  • Editorial Team
    Editorial Team added an answer NHibernate supports DateTime, Ticks, TimeSpan and Timestamp. Make sure you… May 11, 2026 at 9:56 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.