티스토리 뷰

문자 분류 데이터 변환 관련 라이브러리

# 라이브러리 함수 이해

1. 종류
1) 헤더 파일 : string.h
strlen : 문자열의 길이를 반환

int count=0;
while(str[i]!=NULL) {
   count++;
   i++;
}

2) 복사함수
memcpy : 메모리 블록을 복사
memmove : 메모리 블록을 이동
strcpy : 문자열을 복사
strncpy : 문자열 개수를 지정하여 복사

3) 연결함수
strcat : 문자열을 연결
strncat : 문자열 개수를 지정하여 연결

# 라이브러리 함수 활용

1. 길이함수

#include 
#include 
intmain ()
{
    char szInput[256];
    printf("Enter a sentence: ");
    gets (szInput);
    printf("The sentence entered is %u characters long.\n",(unsigned)strlen(szInput));
    return 0;
}

// 결과값
Enter sentence: just testing
The sentence entered is 12 characters long.
2. 복사함수

#include 
#include 
intmain( void)
{
    char *ptr_sour= “Good Morning!";
    char *ptr_dest;
    ptr_dest= (char *)malloc( 20);
    memcpy( ptr_dest, ptr_sour, strlen( ptr_sour)+1); // NULL까지포함하기위해+1
    printf( "%s\n", ptr_dest);
    free( ptr_dest);
    return 0;
}

// 결과값
Good Morning!

#include 
#include 
int main ()
{
    char str[] = "memmove can be very useful......";
    memmove (str+20,str+15,11);
    puts (str);
    return 0;
}

//결과값
memmove can be very very useful.

#include 
#include 
int main ()
{
    char str1[]="Sample string";
    char str2[40];
    char str3[40];
    strcpy (str2,str1);
    strcpy (str3,"copy successful");
    printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
    return 0;
}

// 결과값
str1 : Sample string
str2 : Sample string
str3 : copy successful
3. 연결함수

#include 
#include 
int main ()
{
    char str[80];
    strcpy (str,"these ");
    strcat (str,"strings ");
    strcat (str,"are ");
    strcat (str,"concatenated.");
    puts (str);
    return 0;
}
                
// 결과값
these strings are concatenated. 
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함