#include<stdio.h>
//1. 문자 길이
int strlen(char* s)
{
char* temp = s;
while (*++temp)
; /* not thing */
return temp - s;
}
//2. 문자열 복사. source -> dest
void strcpy(char* dest, char* source)
{
while (*dest++ = *source++)
; /*not thing*/
}
//3. strcmp(char * dest, char* source)
int strcmp(char* dest, char* source)
{
for (; *dest && *source; dest++, source++){
; /* not thing*/
}
if (*source - *dest > 0){
return 1;
}
else if (*source - *dest < 0){
return -1;
}
else
return *source - *dest;
}
// ASCII TO INTEGER 4. atoi(char *) : 문자 -> 숫자.
int atoi(char* source){
int num = 0;
for (; *source; source++)
{
num = 10 * num + *source - '0';
}
return num;
}
//5. integer to ascii : 숫자 -> 문자
void itoa(int num, char* src)
{
//거꾸로 복사하고
//num = 12345;
char* temp = src;
while (num > 0)
{
char c = num % 10 + '0';
*temp++ = c;
num = num / 10;
}
printf("text: %s \n", src);
//리버스.
//src = 54321
//14325 -> 12345
for (; --temp - src > 0; temp, src++)
{
char c = *src;
*src = *temp;
*temp = c;
}
}
void main()
{
char ch[10] = "1234";
char temp[10] = "ab";
printf("strlen(%s): %d \n", ch, strlen(ch));
//strcpy(temp, ch);
printf("strcpy(%s, temp) = > temp : %s \n", ch, temp);
printf("strcmp(%s, %s) : %d \n", ch, temp, strcmp(ch, temp));
printf("atoi(%s): %d \n", ch, atoi(ch));
itoa(12340, ch);
printf("itoa(12340, ch): %s \n", ch);
}
'Computer Language > c 언어' 카테고리의 다른 글
strcmp 활용 (0) | 2014.04.10 |
---|---|
NULL == '0' == 0 (0) | 2014.04.10 |
구조체 배열 (0) | 2014.04.09 |
열거자 에러를 선언시 (0) | 2014.04.09 |
열거자 (0) | 2014.04.09 |