gets()
- Get String의 약자.
gets(입력된 문자열이 들어갈 변수);
키보드로 입력된 문자를 변수에 저장하는 함수. |
#include <stdio.h>
void main(void){
char testText[100];
printf("임의의 문자열을
입력하세요.\n");
gets(testText);
puts(testText); //Put
String의 약자
} |
atoi()
- Alphabet to Integer의 약자.
atoi(숫자로 변환할 문자열);
문자를 숫자로 변환시켜주는 함수. |
#include <stdio.h>
#include <stdlib.h>
void main(void){
char testText[100];
int num;
printf("임의의 정수를
넣으세요.\n");
gets(testText);
num = atoi(testText);
printf("%d 더하기
%d는 %d.\n", num, num, num+num);
} |
strcpy()
- String Copy의 약자.
strcpy(복사될 곳의 문자열 포인터, 복사 원본이 될 문자열 포인터);
문자열을 복사하는 함수. |
#include <stdio.h>
#include <string.h>
void main(void){
char testText[100];
strcpy(testText,
"String copy test...");
puts(testText);
}
*C 프로그램속에서 "에 둘러싸인 문자열은
그 문자열의 포인터란 의미. |
strncpy()
strncpy(복사될 곳의 문자열 포인터, 복사 원본이 될 문자열 포인터, 최대 복사길이);
복사할 길이를 지정하고 문자열을 복사하는 함수. |
#include <stdio.h>
#include <string.h>
void main(void){
char testText[100];
strncpy(testText,
"String copy test...", 7 );
/*
7글자만 복사. */
testText[7] = '\0';
puts(testText);
}
|
strcmp()
- String Compare의 약자.
strcmp(비교할 문자열의 포인터, 비교할 문자열의 포인터);
두 문자열을 비교하는 함수.
비교한 두 문자열이 같으면 0을, 아니면 다른 값을 반환한다. |
#include <stdio.h>
#include <string.h>
void main(void){
char pass[20];
printf("password를
넣으세요.\n");
gets(pass);
if (strcmp(pass,
"1234")==0){
puts("password가
일치.");
} else {
puts("password가
불일치.");
}
} |
strcat()
strcat(연결할 문자(앞), 연결할 문자(뒤));
두 문자열을 연결하는 함수. |
#include <stdio.h>
#include <string.h>
void main(void){
char testText[100];
strcpy(testText, "First
text ");
strcat(testText,
"and Second Text.\n");
puts(testText);
} |
strlen()
-String Length의 약자.
strlen(길이를 측정할 문자열);
문자열의 길이를 측정하는 함수. |
#include <stdio.h>
#include <string.h>
void main(void){
char testText[100];
int length;
printf("임의의 문자열을
입력하세요.\n");
gets(testText);
length = strlen(testText);
printf("입력된 문자열 \"%s\"의
길이는 %d.\n", testText, length);
} |
sprintf()
sprintf(문자열을 넣을 곳, 생성될 문자열, 인수);
형식을 지정하여 문자열을 출력하는 함수. |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(void){
int age;
char agestr[20];
char name[30];
char job[30];
char person[200];
puts("나이를 입력하세요.");
gets(agestr);
age = atoi(agestr);
puts("이름을 입력하세요.");
gets(name);
puts("직업을 입력하세요.");
gets(job);
sprintf(person,
"이름: %s, 나이: %s. 직업: %s\n", name, age, job);
puts(person);
} /* visual C++에서 컴파일엔 에러 없지만 실행시 에러 발생. */ |