[임베디드 C] 문자열 파싱

정재훈·2022년 3월 30일
0

임베디드 C언어

목록 보기
2/11
post-thumbnail

파싱의 개념

파싱은 시각화 및 차트화를 위해 자주 사용됩니다. 로그데이터를 시각화, Law Data를 가공하는것
임베디드에서는 string.h 라이브러리만 주로 사용함, 오직 C언어로만

String.h Lib 문법

strlen

문자열의 길이 구하는 메서드

#include <stdio.h>
#include <string.h>

int main() {

	char vect[10] = "ABCDEFG";

	printf("%d ", strlen(vect));  // 7
	printf("%d ", strlen(vect+2)); // 5
}

strncpy

strncpy(복사될 주소, 복사할 주소, n) : 복사할 주소의 문자열의 0번 index부터 n번 index까지 복사하여 복사될 주소의 문자열에 복사하는 메서드

#include <stdio.h>
#include <string.h>

int main() {

	char temp[10];
	char buf[10] = "AASDRW";
	strncpy(temp,buf,5);
	printf("%s",temp); // AASDR
}

strcpy

strcpy(문자열 1, 문자열 2) : 문자열 1에 문자열 2를 끝 index까지 복사한다.

#include <stdio.h>
#include <string.h>


int main()
{
	char vect[10] = "ABCDEFG";
	char temp[10];
	strcpy(temp,vect+2); 
	printf("%s",temp); // CDEFG

	return 0;
}

strcat

문자열을 붙이는 함수로, strcat(문자열 1, 문자열 2) : 문자열 1 = 문자열 1 + 문자열 2

#include <stdio.h>
#include <string.h>


int main()
{
	char hello[10] = "HELLO";
	char world[10] = "WORLD";

	strcat(hello,world);

	printf("%s",hello); // HELLOWORLD


	return 0;
}

strcmp

두 문자열 비교 함수로,
두 문자열이 같다면 0
앞에 문자열이 사전순으로 앞쪽에 위치해 있으면 -1
앞에 문자열이 사전순으로 뒤쪽에 위치해 있으면 +1

문자열이 같다

#include <stdio.h>
#include <string.h>


int main()
{
	const char* v1 = "ABC";
	const char* v2 = "ABC";

	printf("%d", strcmp(v1,v2)); // 0


	return 0;
}

앞 문자열이 사전순으로 더 뒤에 있다

#include <stdio.h>
#include <string.h>


int main()
{
	const char* v1 = "ABCD";
	const char* v2 = "ABC";

	// ABCD < ABC  ,  ABD < ABC
	printf("%d", strcmp(v1,v2)); // 1


	return 0;
}

앞 문자열이 사전순으로 더 앞에 있다

#include <stdio.h>
#include <string.h>


int main()
{
	const char* v1 = "AAE";
	const char* v2 = "aae";

	// ABC > ABCD , AAE > aae , ABC > ABD , AZZ > BAA
	printf("%d", strcmp(v1,v2)); // -1


	return 0;
}

sscanf

sscanf는 문자열을 여러 변수로 쪼깰 때 유용하게 사용합니다.

#include <string.h>
#include <stdio.h>

int main()
{
	char vect[30] = "[152.123459] message 24";

	float time;
	char msg[30];
	int num;

	sscanf(vect, "[%f]%s%d", &time,msg,&num);

	printf("%f\n",time); // 152.123459
	printf("%s\n",msg); // message
	printf("%d\n",num); // 24


	return 0;
}

sprintf

변수를 하나의 문자열로 형식에 맞게 합칠 때 사용합니다.

#include <string.h>
#include <stdio.h>

int main()
{
	float time = 30.415253;
	char msg[30] = "HI jaehoon";
	int num = 35;

	char result[30];

	sprintf(result, "[%.6f] %s %d", time,msg,num);

	printf("%s",result); // [30.415253] HI jaehoon 35


	return 0;
}

atoi

stdlib.h를 사용하는 것이다. 문자열을 정수형으로 변환시켜주는 함수

#include <stdlib.h>
#include <stdio.h>

int main()
{
	char first[10] = "4125";
	char second[10] = "1234";

	printf("%d",atoi(first) + atoi(second));

	return 0;
}

strtok

문자열 split하는 함수로, strtok(문자열, 기준); : 문자열에서 기준을 찾아 기준 전 index까지 문자열을 출력해주는 함수입니다.
만약 기준을 찾았다면 해당 기준은 NULL(\0) 값으로 변경하게 됩니다.

#include <stdio.h>
#include <string.h>

int main()
{
	char buf[10] = "AA#BB#CC";
	
    // NULL => '\0'로 대체 가능하다.
	for(char *p = strtok(buf,"#"); p != NULL; p = strtok(NULL, "#"))
	{
		printf("%s",p);    // AABBCC
	}


	return 0;
}

strchar

strchar(문자열, 찾을 문자) : 문자열에서 해당 문자를 찾고, 찾은 문자의 시작 주소를 리턴해줍니다. 못 찾을 경우 NULL('\0')을 리턴해 줍니다.

#include <stdio.h>
#include <string.h>

int main()
{
	char vect[10] = "ABCDEFG";

	printf("%s ", strchr(vect,'C')); // CDEFG

	return 0;
}

strstr

strstr(문자열, 찾을 문자열) : 문자열에서 찾을 문자열을 찾고, 찾은 문자열의 맨 처음 시작 주소를 리턴해주고, 못찾을 경우 NULL('\0')을 리턴해 줍니다.

#include <stdio.h>
#include <string.h>

int main()
{
	char vect[10] = "ABCDEFG";

	printf("%s ", strstr(vect,"BCD")); // BCDEFG

	return 0;
}
profile
여러 방향으로 접근하는 개발자

0개의 댓글