42Seoul/libft

libft - toupper, tolower, strchr

재윤 2023. 3. 24. 11:07
반응형

toupper

함수원형

int	ft_toupper(int c)

toupper 라이브러리는 소문자를 대문자로 변경해서 반환하며, 다른 모든 문자는 그대로 반환

  1. 소문자를 대문자로 변경
  2. 다른 모든 문자는 그대로 반환

int형으로 받는데 캐스팅 해서 넣어준다음 돌려주면 됨. 굳이 캐스팅 안 해도 됨

#include "libft.h"

int	ft_toupper(int c)
{
	if (c >= 'a' && c <= 'z')
		c = c - 32;
	return (c);
}

int main()
{
	char str1 = 'a';
	// char str2[15] = "abc";
	printf("%d\\n", ft_toupper(str1));
	
	// char str3[10] = "abc";
	// char str4[15] = "abc";
	// printf("%d\\n", strncmp(str3, str4, 3));
}

 

참고 사이트

[C언어/C++] tolower, toupper 대문자 소문자 변경

 

[C언어/C++] tolower, toupper 대문자 소문자 변경

안녕하세요. BlockDMask 입니다. 오늘은 C언어, C++에서 알파벳을 소문자는 대문자로, 대문자는 소문자로 변경해주는 tolower, toupper 함수에 대해서 알아보려고 합니다. 1. toupper, tolower 함수 원형과 사

blockdmask.tistory.com

 

tolower

tolower 라이브러리는 대문자를 소문자로 변경해서 반환하며, 다른 모든 문자는 그대로 반환

  1. 대문자 → 소문자
  2. 다른 모든 문자 그대로 반환

int형으로 받는데 캐스팅 해서 넣어준다음 돌려주면 됨. 굳이 캐스팅 안 해도 됨

함수원형

int	ft_tolower(int c)
#include "libft.h"

int	ft_tolower(int c)
{
	if (c >= 'A' && c <= 'Z')
		c = c + 32;
	return (c);
}
int main()
{
	char str1 = 'a';
	// char str2[15] = "abc";
	printf("%d\\n", ft_tolower(str1));
	
	// char str3[10] = "abc";
	// char str4[15] = "abc";
	// printf("%d\\n", strncmp(str3, str4, 3));

}

참고 사이트

[C언어/C++] tolower, toupper 대문자 소문자 변경

 

[C언어/C++] tolower, toupper 대문자 소문자 변경

안녕하세요. BlockDMask 입니다. 오늘은 C언어, C++에서 알파벳을 소문자는 대문자로, 대문자는 소문자로 변경해주는 tolower, toupper 함수에 대해서 알아보려고 합니다. 1. toupper, tolower 함수 원형과 사

blockdmask.tistory.com

 

strchr

함수원형

char	*ft_strchr(const char *s, int c)

문자열 찾는 라이브러리임.

  1. 문자열 s를 받아와서 그 문자열 안에 c가 있으면 c가 있는 위치의 주소값을 리턴.
  2. 만약 문자를 찾지 못 하면 NULL 반환

뒤에 똑같은 문자가 있어도 앞에 있는 친구 주소값 리턴.

예외 처리

(1)c에 널값이 들어왔을 때 처리를 해주어야함.

ca에 널값일 때 처리

(2)c에 확장 아스키 코드 값이 들어올 수 있기에 unsigned char 캐스팅

#include "libft.h"

char	*ft_strchr(const char *s, int c)
{
	char			*temp;
	int				count;
	unsigned char	ca;

	temp = (char *)s;
	ca = (unsigned char)c;
	if (ca == '\\0')
	{
		count = ft_strlen(s);
		return (temp + count);
	}
	while (*temp)
	{
		if (*temp == ca)
			return (temp);
		temp++;
	}
	return (0);
}

int main()
{
	char dest[] = "bonjour";
    char *first;
    // char *last;

	/* dest 문자열에서 첫번째 나오는 'b'문자를 찾습니다. */
	first = ft_strchr(dest, '\\0');

	// /* dest 문자열에서 마지막 나오는 'b'문자를 찾습니다. */
	// last = strrchr(dest, 'b');
	
	 printf("first : %s \\n", first);
	// printf("last : %s", last);
    
	return 0;
	
	}

참고 사이트

strchr, strnstr, strncmp 함수 구현

 

strchr, strnstr, strncmp 함수 구현

틀린 내용이 있다면 댓글로 알려주세요! 감사합니다 :) 💡 strchr에 대하여 환경 c, c++ C에서는 C++에서는 Prototype char *ft_strchr(const char *str, int ch); str : c 형식 문자열 ch : 검색할 문자로, int 형태로 형

minsoftk.tistory.com

 

반응형

'42Seoul > libft' 카테고리의 다른 글

libft - strnstr, atoi, calloc  (0) 2023.03.24
libft - strrchr, memchr, memcmp  (2) 2023.03.24
libft - strlcpy, strlcat, strncmo  (0) 2023.03.24
libft - bzero, memcpy, memove  (0) 2023.03.24
libft - isascii, isprint, memset  (0) 2023.03.24