Javaで2つの文字列を比較するプログラム
この記事では、2つの文字列を比較する方法を理解します。 2つの文字列の比較は、算術演算子「==」を使用して実行できます。文字列は文字のシーケンスです。 Javaプログラミング言語では、文字列はオブジェクトとして扱われます。
以下は同じのデモンストレーションです-
入力がであると仮定します −
Second string : Java Program First string :Java
必要な出力は −
The result of string comparison is: 8
アルゴリズム
Step 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Compute the lengths of the strings. Find the minimum of this length. Assign it to a variable. Step 5 - Iterate though this value and find the character at every index in both the strings. Convert them to integer, and compare these characters. Step 6 - Find the integer difference between these characters and return it as the output. Step 7 - Display the result Step 8 - Stop
例1
ここでは、「main」関数の下ですべての操作をバインドします。
public class CompareStrings {
public static void main(String args[]) {
String input_string_1 = new String("Java Program");
System.out.println("The string is defined as: " +input_string_1);
String input_string_2 = new String("Java");
System.out.println("The string is defined as: " +input_string_2);
int length_1 = input_string_1.length();
int length_2 = input_string_2.length();
int minimum_length = Math.min(length_1, length_2);
int result;
for (int i = 0; i < minimum_length; i++) {
int character_1 = (int)input_string_1.charAt(i);
int character_2 = (int)input_string_2.charAt(i);
if (character_1 != character_2) {
result = character_1 - character_2;
}
}
if (length_1 != length_2) {
result = length_1 - length_2;
} else {
result = 0;
}
System.out.println("\nThe result of string comparison is: " +result);
}
} 出力
The string is defined as: Java Program The string is defined as: Java The result of string comparison is: 8
例2
ここでは、操作をオブジェクト指向プログラミングを示す関数にカプセル化します。
public class CompareStrings {
public static int string_compare(String input_string_1, String input_string_2) {
int length_1 = input_string_1.length();
int length_2 = input_string_2.length();
int minimum_length = Math.min(length_1, length_2);
for (int i = 0; i < minimum_length; i++) {
int character_1 = (int)input_string_1.charAt(i);
int character_2 = (int)input_string_2.charAt(i);
if (character_1 != character_2) {
return character_1 - character_2;
}
}
if (length_1 != length_2) {
return length_1 - length_2;
} else {
return 0;
}
}
public static void main(String args[]) {
String input_string_1 = new String("Java Program");
System.out.println("The string is defined as: " +input_string_1);
String input_string_2 = new String("Java");
System.out.println("The string is defined as: " +input_string_2);
System.out.println("\nThe result of string comparison is: " +string_compare(input_string_1, input_string_2));
}
} 出力
The string is defined as: Java Program The string is defined as: Java The result of string comparison is: 8
-
strncmpライブラリ関数を使用して2つの文字列を比較するCプログラムを作成します
Strncmpは、string.hファイルに存在する事前定義されたライブラリ関数であり、2つの文字列を比較し、どちらの文字列が大きいかを表示するために使用されます。 strcmp機能(文字列比較) この関数は2つの文字列を比較します。両方の文字列の最初の2つの一致しない文字のASCIIの違いを返します。 構文 int strcmp (string1, string2); 差がゼロに等しい場合、string1=string2。 string2。 差が負の場合、string1
-
C++で2つのバイナリ文字列を追加するプログラム
2進数の文字列が2つある場合、それら2つの2進数文字列を加算して得られた結果を見つけ、その結果を2進数文字列として返す必要があります。 2進数は、0または1のいずれかで表される数値です。2つの2進数を加算する際には、2進数の加算規則があります。 0+0 → 0 0+1 → 1 1+0 → 1 1+1 → 0, carry 1 入力 str1 = {“11”}, str2 = {“1”} 出力 “100” 入力 str1 = {“110”},