본문 바로가기
Python, C, C++

strcmp [C][C++][python]

by 소나무기운 2022. 4. 4.
반응형

[2022/04/05] Insert title image

[2022/04/05] Add English subtitles

[2022/04/05] Add C++ example

[2022/04/04] First Start.

소나무 기운 ,  전자제품 개발/생산

strcmp() [C][C++][python]

strcmp() 함수를 이용하여 두 개의 문자열을 비교하는 방법을 알아보자.

strcmp() 함수는 두개의두 개의 문자열을 서로 비교하여 두 개의 문자열이 같으면 0을 반환한다.

두 개의 문자열이 같지 않으면 0이 아닌 값을 반환한다.

 

Let's find out hot to complare two strings using the strcmp() function.

The strcmp() function compares the two stings to each other.

Returns 0 if the two strings are the same.

Returns a non-zero value if the two stings are not the same.

 

이 strcmp()함수는 string.h 헤더 파일이 들어있다.

 

This strcmp() function contains the string.h header file.

 

prototype

strcmp() 함수는 이렇게 생겼다.

strcmp() looks like this.

int strcmp (const char* str1, const char* str2 );

 

 

Parameters

이 함수는 두개의 인자를 갖는다.

This function has two factors.

  • str1 - a string
  • str2 - a starng

 

 

Return value

이 함수의 반환값이다.

The return value of this function.

반환 값
Return value
상세 설명
detailed description
0 두 문자열이 같을 때
when the two strings are the same

>0 첫번째 불일치 하는 문자가 다르고 str1의 문자가 str2의 문자값 보다 클 경우 (ASCII 값을 확인.)
If the first inconsistent character is different and the str1 character is greater than the str2 character value (check the ASCII value).

<0 첫번째 불일치 하는 문자가 다르고 str1의 문자가 str2의 문자값 보다 작을 경우 (ASCII 값을 확인.)
If the first inconsistent character is different and the str1 character is less than the str2 character value (check the ASCII value).

 

 

Example [C]

ㅁ 예제 (example)

#include <stdio.h>
#include <string.h>

int main() {
  char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
  int result;

  // comparing strings str1 and str2
  result = strcmp(str1, str2);
  printf("strcmp(str1, str2) = %d\n", result);

  // comparing strings str1 and str3
  result = strcmp(str1, str3);
  printf("strcmp(str1, str3) = %d\n", result);

  return 0;
}

ㅁ 출력 (output)

strcmp(str1, str2) = 1
strcmp(str1, str3) = 0

 

Example [C++]

ㅁ 예제 (example)

#include <cstring>
#include <iostream>
using namespace std;

int main() {
	char stringA[] = "abc";
	char stringB[] = "abd";

	// returns -1 because "abc" < "abd"
	int result = strcmp(stringA, stringB);

	cout << "Comparing " << stringA << " and " << stringB << ": " << result << endl;

	// returns 1 because  "abd" > "abc"
	result = strcmp(stringB, stringA);

	cout << "Comparing " << stringB << " and " << stringA << ": " << result << endl;

	// returns 1 because  "abc" = "abc"
	result = strcmp(stringA, stringA);

	cout << "Comparing " << stringA << " and " << stringA << ": " << result;

	return 0;
}

ㅁ 출력 (output)

Comparing abc and abd: -1
Comparing abd and abc: 1
Comparing abc and abc: 0

 

ㅁ 예제 (example)

#include <cstring>
#include <iostream>
using namespace std;

// function to display the result of strcmp()
void compare(char* strFirst, char* strSecond) {

    // compare compare() parameters strFirst and strSecond   
    int result = strcmp(strFirst, strSecond);

    if (result > 0)
        cout << strSecond << " larger then " << strFirst << endl;
    else if (result < 0)
        cout << strSecond << " smaller then " << strFirst << endl;
    else
        cout << strFirst << " equal to " << strSecond <<  endl;
}

int main() {
    char str1[] = "Student";
    char str2[] = "Students";

    compare(str1, str2);

    compare(str2, str1);

    compare(str1, str1);

    compare(str2, str2);

    return 0;
}

ㅁ 출력 (output)

Students smaller then Student
Student larger then Students
Student equal to Student
Students equal to Students

 

Example [Python]

ㅁ 예제 (example)

print( 'abc' == 'abc' ) # True
print( 'abc' == 'abd' ) # False
print( 'abc' == 'ABC' ) # False

ㅁ 결과 (output)

True
False
False

 

Example2 [Python]

ㅁ 예제 (example)

string1 = input('enter first string:\n')
string2 = input('enter second string:\n')

if string1 < string2:
    print(string1 + " comes before " + string2 + " in the dictionary.")
elif string1 > string2:
    print(string1 + " comes after " + string2 + " in the dictionary.")
else:
    print(string1 + " and " + string2 + " are same.")

 

ㅁ 결과 (output)

enter first string:
apple
enter second string:
orange
apple comes before orange in the dictionary.

 

마무리

 

 

참고문헌

 

 

 
 

 

 

틀린 부분이나 질문은 댓글 달아주세요.

즐거운 하루 보내세요. 감사합니다.

 

 

 

반응형

댓글