C함수 문자열 중에 문자 위치 찾기 index()
index()는 문자열 중에 특정 문자의 위치를 찾아 주는 함수입니다.
- 헤더: string.h
- 형태: char *index(const char *s, int c)
- 인수: char *s 검색 대상 문자열
int c 검색할 문자 - 반환: 문자를 찾았으면 문자가 있는 위치를, 찾는 문자가 없다면 NULL을 반환
C언어 index() 함수 예제
#include <stdio.h>
#include <string.h>
int main( void)
{
char *str_sample = "badayak.com";
char *ptr;
ptr = index( str_sample, 'y');
// 'y' 문자를 찬고 그 위치를 ptr 에 반환합니다.
if ( ptr) printf( "%s\n", ptr);
ptr = index( str_sample, 'z');
// 'z' 문자를 찾지 못했으므로 if 절에 걸리지 않습니다.
if ( ptr) printf( "%s\n", ptr);
}
C언어 index() 예제 실행 결과
]$ ./a.out
yak.com
]$
'컴퓨터 > 프로그래밍' 카테고리의 다른 글
C언어 문자열 끝에서부터 문자 위치 검색 함수 rindex() (0) | 2020.03.15 |
---|---|
C언어 프로세스 생성 함수 fork() (2) | 2020.03.15 |
C언어 다른 프로그램 실행 및 대기 함수 system() (0) | 2020.03.15 |