ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C]두 개의 문자열 연결, 비교 함수
    @떤떤/#C,C++ 2020. 3. 20. 20:53

    <두 개의 문자열을 연결하는 함수>

     

    #include<stdio.h>
    #include<string.h>
    int main(void) {
    	char s[15] = "maple";
    	char t[6] = "story";
    	strcat(s, t);
    	printf("strcat:%s\n", s);
    	strncat(s, t, 3);
    	printf("strncat:%s\n", s);
    	return 0;
    }

     

     

    char *strcat(char *s1, const char *2)

    -문자열 s2를 문자열 s1에 연결하고 문자열 s1을 반환

    char *strncat(char *s1,const char *2,size_t n)

    -문자열 s2의 (처음부터)n개의 문자열을 문자열 s1에 연결하고 문자열 s1을 반환

     

     

    실행결과

     

     

     

    <두 개의 문자열을 비교하는 함수>

     

    #include<stdio.h>
    #include<string.h>
    int main(void) {
    	char *s1 = "Republic of KOREA";
    	char *s2 = "Republic of CHINA";
    	int ptr;
    	ptr = strcmp(s1, s2);
    	if (ptr < 0 || ptr>0)
    		printf("문자열 s1과 s2가 다르다\n");
    	ptr = strncmp(s1, s2, 12);
    	if (ptr == 0) {
    		printf("문자열 s1과 s2가 같다.\n");
    	}
    	return 0;
    }

     

     

     

    int strcmp(const char *s1,const char *s2)

    -문자열을 알파벳 순서로 비교

    -두 문자열이 같으면 0 반환, s1<s2 음수값 반환, s1>s2 양수값 반환

    int strncmp(const char *s1, const char *s2)

    -두 문자열 (처음부터)n개의 문자열을 비교

    -두 문자열이 같으면 0 반환, 같지 않으면 strcmp와 같은 값 반환

     

     

    실행결과

     

    댓글

Designed by Tistory.