Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- 코스닥
- Project
- 개인프로젝트
- Reflex
- 개발자
- 자바정리
- frontend
- 자바
- 코딩
- 코딩테스트
- 모의 주식
- 프로그래밍
- backend
- SNS
- java
- 백준
- 프로그래머스
- 사물인터넷
- C++
- 이웃한 칸
- 빅데이터
- 프로그램
- java정리
- 자바독학
- 풀스택프레임워크
- 코테
- 코딩독학
- Python
- 개발
- 주가예측
Archives
- Today
- Total
Creative Code
main.c(문자열 라이브러리) 본문
※main.c파일
#include <stdio.h>
#include "string.h"
int main(void)
{
char title1[] = "wonderful tonight";
int len = my_strlen(title1); // 문자열의 길이
printf("len : %d\n", len); //메모리에 할당된 공간은 18바이트지만 문자열의 길이는 17바이트로 출력
char title2[my_strlen(title1)+1];
my_strcpy(title2,title1); //문자열 복사
if (my_strcmp(title1,title2) == 0 ) { // 문자열 비교 결과가 0이면 같음
printf("title1 and title2 are equal\n");
} else {
printf("title1 and rtitle2 are not equal\n");
}
char result[100];
my_strcpy(result,title1);
my_strcat(result," - "); // 문자열 합치기
my_strcat(result, "eric clapton");
printf("result : %s\n",result);
return 0;
}
※string.h 파일
#ifndef STRING_H
#define STRING_H
int my_strlen(const char *str);
int my_strcmp(const char *str1,const char *str2);
void my_strcpy(char *str1,const char *str2);
void my_strcat(char *str1,const char *str2);
#endif
※string.c파일
#include <stdio.h>
int my_strlen(const char *str)
{
int index = 0;
while (str[index]!='\0') {
index++;
}
return index;
}
int my_strcmp(const char *str1, const char *str2) {
int result = 0;
for (int i = 0; i<my_strlen(str1); i++){
if (str1[i] != str2[i]){
result = 1;
break;
}
}
return result;
}
void my_strcpy(char *str1, const char *str2) {
for (int i = 0; i<=my_strlen(str2); i++){
str1[i] = str2[i];
}
}
void my_strcat(char *str1,const char *str2) {
my_strcpy(str1 + my_strlen(str1),str2);
}
'C Programming' 카테고리의 다른 글
hello7.c(문자열 배열-포인터) (0) | 2023.08.08 |
---|---|
atoi.c(문자열을 숫자로 출력) (0) | 2023.08.07 |
main.c(구조체 포인터,파일 입출력 사용 성적순 정렬) (0) | 2023.08.07 |
score.dat(점수 데이터) (0) | 2023.08.07 |
name.c( 문자열 포인터로 입력) (0) | 2023.08.07 |