How to check if two strings are equal "regardless of one character"?

how to compare two strings in java using if
how to compare two strings in c++
how to compare characters in a string in java
check if two strings have same characters in java
how to compare two strings in java without using equals
java string equals
java string not equal
java compare strings alphabetically

I want to check if two strings are equal regardless of one character is missing. For example "HappyMan" equals "HappyMn" and "HappyMann" and "HappyMnn". Is there any built in function to do this task? I've tried to sort both of them then count how many characters are different but it doesn't work. Here is my code

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

int main() {
    string a, b;
    cin >> a >> b;
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    int cnt = 0;
    for (int i = 0; i < a.length(); i++) {
        if (a[i] != b[i])
            cnt++;
        if (cnt > 1) {
            cout << "Not equal";
            return 0;
        }
    }
    cout << "Equal";

    return 0;
}
#include <iostream>
#include <string>
int main()
{
  std::string str1 = "manatee";
  std::string str2 = "mannatee";

  std::string strLong = "";
  std::string strShort = "";

  if (str1.length() >= str2.length()){
      strLong = str1;
      strShort = str2;
  }
  else{
      strLong = str2;
      strShort = str1;
  }

  if (strLong.length() - strShort.length() > 1){
      printf("%s", "They are not equal.");
      return false;
  }

  bool firstDifference = true;

  for (int i = 0; i < strShort.length(); i++){
    if (strShort[i] != strLong[i] && firstDifference){
        if (strLong.length() > strShort.length()){
            strLong = strLong.substr(0, i) + strLong.substr(i+1, strLong.length() - i 
            - 1);
        }
        firstDifference = false;
        continue;
    }
    else if (strShort[i] != strLong[i] && !firstDifference) {
        printf("%s", "They are not equal");
        return false;
    }    
  }

  printf("%s", "They are equal");
  return true;

}

How do I compare strings in Java?, equals() so you don't have to (available as of JDK7, also available in Guava). Consequently, if you want to test whether two strings have the same value you will  If you need to compare two text strings in Excel to determine if they're equal, you can use the EXACT function. For example, if you want to compare A2 with B2, use: = EXACT ( A2 , B2 )

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <unordered_map>
using namespace std;

int main() {
    string a, b;
    cin >> a >> b;
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    unordered_map<char, bool> umap;
    int cnt = 0;
    string longestStr = "";
    string shortestStr = "";
    if(a.size() > b.size()) {
        longestStr = a;
        shortestStr = b;
    }
    else {
        longestStr = b;
        shortestStr = a;
    }
    for (int i = 0; i < longestStr.size(); i++)
       umap[longestStr[i]] = true;

    for (int i = 0; i<shortestStr.size();i++) {
        if (umap[shortestStr[i]] == false) {
            cnt++;
        }
        if(cnt > 1){
            cout << "Not Equal";
            return 0;
        }
    }

    cout << "Equal";
    return 0;
}

Program to check if two strings are same or not, Given two strings, the task is to check if these two strings are identical(same) or not. Examples: Input: string1 = “GeeksforGeeks”, string2 = “GeeksforGeeks” Objective– Given two strings, find out if they are equal or not without using any built-in function (without using equals() function in java).. Example:. String x='tutorial' and String y='tutorial' are equal - true String x='tutorial' and String y='tutorial ' are equal - false String x='tutorial' and String y=' ' are equal – false

int main()
{
  std::string str1 = "iceman";
  std::string str2 = "icemannn";

  std::string strLong = "";
  std::string strShort = "";

  if (str1.length() >= str2.length()){
      strLong = str1;
      strShort = str2;
  }
  else{
      strLong = str2;
      strShort = str1;
  }

  if (strLong.length() - strShort.length() > 1){
      printf("%s", "They are not equal.");
      return false;
  }

  bool firstDifference = true;

  for (int i = 0; i < strShort.length(); i++){
    if (strShort[i] != strLong[i] && firstDifference){
        printf("%s %i", "first difference found at ", i);
        strLong = strLong.substr(0, i) + strLong.substr(i+1, strLong.length() - i - 
1);
        firstDifference = false;
        continue;
    }
    else if (strShort[i] != strLong[i] && !firstDifference) {
        printf("%s", "They are not equal");
        return false;
    }    
  }

  printf("%s", "They are equal");
  return true;

}

Compare Two Strings, About Compare Two Strings. This tool will compare two strings and check whether two strings are the same or not. Related. String Repeat · Get String Length  To check if two strings are equal in bash scripting, use bash if statement and double equal to == operator. To check if two strings are not equal in bash scripting, use bash if statement and not equal to != operator. Example – Strings Equal Scenario Following is an example program to check if two strings are equal in Bash Scripting

Comparing Strings in Java, This method returns 0 if two Strings are equal or if both are null, a static equals​() method, useful in this scenario – to compare two Strings. Count of strings that become equal to one of the two strings after one removal; Pairs of complete strings in two sets of strings; Number of common base strings for two strings; Distinct strings such that they contains given strings as sub-sequences; Strings in C; XOR of two Binary Strings; Add n binary strings <strings> library in C++ STL

String::equals() \ Language (API) \ Processing 3+, Compares two strings to see if they are the same. This method is necessary because it's not possible to compare strings using the equality  The String equals method looks at the two Java strings, and if they contain the exact same string of characters, they are considered equal. Taking a look at a quick String comparison example with the equals method, if the following test were run, the two strings would not be considered equal because the characters are not the exactly the same

equals() Java, equals() java. This is a method which checks whether two strings are equal or not​. In this article you will see how it works on the example. This means that if you call the equals() method to compare 2 String objects, then as long as the actual sequence of characters is equal, both objects are considered equal. The == operator checks if the two strings are exactly the same object. The .equals() method check if the two strings have the same value.

Comments
  • Read about Levenshtein distance
  • "How do I do X - here's some code" isn't an acceptable stack overflow question. You need to provide exactly what doesn't work with your code.
  • Supposedly your algorithm is wrong: a lot of strings become equal after sorting, so your algorithm cannot do what you've described. Here's one which should work: find the first non-matching character, and then skip it in the string which is longer. Then the remaining part must match.
  • I fixed it, I hadn't understood your question but now it should be fine.