본문으로 건너뛰기
  1. Memo/

C언어 string.h 관련 메모

·
C
작성자
hw5e
page.hw5e.cc

1학년 2학기 수업 때 다뤘던 것

#include <stdio.h>
#include <string.h>             // 문자열 처리 함수 사용 시 필요하다.
int main(void) {
    char str[10] = "hello";     // 크기가 10인 문자 배열 선언
    int len = 0;
                                                               
    printf("str의 길이: %d\n", strlen(str));       // 널 문자를 제외한 문자열의 길이
    printf("\"good bye\"의 길이: %d\n", strlen("good bye"));   // 문자열 리터럴의 길이

    printf("str = %s\n", str);
    len = strlen(str);
    if (len > 0)                // 널 문자열이 아니면
        str[len - 1] = '\0';    // 마지막 한 글자를 삭제한다.
    printf("str = %s\n", str);
    return 0;
}

str의 길이: 5 “good bye"의 길이: 8 str = hello str = hell

strlen() 함수 strlen(“문자열”)/strlen(문자열을 가리키는 주소) 해당 문자열의 길이를 반환함 문자열의 끝에 있는 NULL은 포함하지 않는다

#include <stdio.h>
#include <string.h>             // 문자열 처리 함수 사용 시 포함
int main(void) {
    char str1[10] = "";         // 널 문자열로 초기화한다.
    char str2[10] = "";         // 널 문자열로 초기화한다.
    char temp[10];
   
    printf("2개의 문자열? ");
    scanf("%s %s", str1, str2); // 빈칸으로 구분해서 문자열 입력
    printf("str1 = %s, str2 = %s\n", str1, str2);

    // 두 문자 배열을 swap한다.
    strcpy(temp, str1);         // str1을 temp로 복사한다.
    strcpy(str1, str2);         // str2을 str1로 복사한다.
    strcpy(str2, temp);         // temp을 str2로 복사한다.
    printf("str1 = %s, str2 = %s\n", str1, str2);
    return 0;
}

strcpy() 함수 strcpy(주소1,주소2) 주소 2가 가리키고 있는 문자열을 주소1을 시작주소로 하는 변수에 복사한다

#include <stdio.h>
#include <string.h>             // 문자열 처리 함수 사용 시 필요하다
int main(void) {
    char s1[10] = "apple";
    char s2[10] = "apple";

    if (s1 == s2)               // s1과 s2의 주소가 같은지 비교한다.
        printf("s1과 s2의 주소가 같습니다.\n");
    else
        printf("s1과 s2의 주소가 다릅니다.\n");

    if (strcmp(s1, s2) == 0)    // s1과 s2의 내용이 같은지 비교한다.
        printf("s1과 s2의 내용이 같습니다.\n");
    else
        printf("s1과 s2의 내용이 다릅니다.\n");
    return 0;
}

s1과 s2의 주소가 다릅니다. s1과 s2의 내용이 같습니다.

strcmp() 함수 주소 1이 가리키고 있는 곳의 문자열과 주소2가 가리키고 있는 문자열이 동일한지 비교한다 비교하는 두 문자열이 동일한 경우 0을 반환한다 동일하지 않은 경우 양수나 음수를 반환한다

#include <stdio.h>
#include <string.h>             // 문자열 처리 함수 사용 시 필요하다
int main(void) {
    char s1[10] = "Apple";
    char s2[10] = "apple";

    if (s1 == s2)               // s1과 s2의 주소가 같은지 비교한다.
        printf("s1과 s2의 주소가 같습니다.\n");
    else
        printf("s1과 s2의 주소가 다릅니다.\n");

    if (strcmp(s1, s2) == 0)    // s1과 s2의 내용이 같은지 비교한다.
        printf("s1과 s2의 내용이 같습니다.\n");
    else
        printf("s1과 s2의 내용이 다릅니다.=> %d\n",strcmp(s1,s2));
    return 0;
}

s1과 s2의 주소가 다릅니다. s1과 s2의 내용이 다릅니다.=> -1

#include <stdio.h>
#include <string.h>             // 문자열 처리 함수 사용 시 필요하다
int main(void) {
    char s1[10] = "apple";
    char s2[10] = "Apple";

    if (s1 == s2)               // s1과 s2의 주소가 같은지 비교한다.
        printf("s1과 s2의 주소가 같습니다.\n");
    else
        printf("s1과 s2의 주소가 다릅니다.\n");

    if (!strcmp(s1, s2))    // s1과 s2의 내용이 같은지 비교한다.
        printf("s1과 s2의 내용이 같습니다%d.\n");
    else
        printf("s1과 s2의 내용이 다릅니다.=> %d\n",strcmp(s1,s2));
    return 0;
}

s1과 s2의 주소가 다릅니다. s1과 s2의 내용이 다릅니다.=> 1

#include <stdio.h>
#include <string.h>
int main(void) {
    char sentence[100] = "";
    char word[20];

    do {
        printf("단어? ");
        scanf("%s", word);
        strcat(sentence, word);         // 입력받은 단어를 sentence의 끝에 붙인다.
        strcat(sentence, " ");          // 단어를 구분할 수 있도록 빈칸을 붙인다.
    } while (strcmp(word, ".") != 0);   // "."이 입력될 때까지 반복한다.

    printf("%s\n", sentence);
    return 0;
}

단어? null 단어? None 단어? . null None .

strcat() 함수 strcat(주소1,주소2) 주소 1이 가리키고 있는 곳의 문자열의 끝부분에 주소 2가 가리키고 있는 곳의 문자열을 붙인다 주의: 주소1이 가리키고 있는 곳의 길이가 넉넉해야 한다

Reply by Email