059. 문자열을 대·소문자로 구분하여 비교하기(strcmp)059. 문자열을 대·소문자로 구분하여 비교하기(strcmp)

Posted at 2011. 1. 30. 17:58 | Posted in Computer/초보자를 위한 C 언어 300제
#include <stdio.h>
#include <string.h>

#define SKY "sky"

void main( void )
{
	char string[100];
	int ret;

	printf( "영단어를 입력한 후 Enter키를 치세요!\n" );
	printf( "sky를 입력하면 프로그램이 종료됩니다. \n" );

	do
	{
		gets( string );

		ret = strcmp( string, SKY );

		if( ret == 0 )
		{
			printf( "%s == %s, ret = %d \n", string, SKY, ret );
			break;
		}
		else if( ret < 0 ) printf( "%s < %s, ret = %d \n", string, SKY, ret );
		else printf( "%s > %s, ret = %d \n", string, SKY, ret );

	} while( 1 );
}
//

058. 문자열을 복사하는 함수 만들기058. 문자열을 복사하는 함수 만들기

Posted at 2011. 1. 30. 17:52 | Posted in Computer/초보자를 위한 C 언어 300제
#include <stdio.h>

#define KOREA "대한민국"

char* My_strcpy( char* dest, const char* src );

void main( void )
{
	char string[100];

	My_strcpy( string, KOREA );

	puts( string );		// 대한민국을 출력
}

char* My_strcpy( char* dest, const char* src )
{
	if( dest == (int)NULL || src == (int)NULL )
	{
		if( *dest != (int)NULL ) *dest =(int)NULL;
		return NULL;
	}

	do
	{
		*dest++ = *src;
	}
	while( *src++ != (int)NULL );

	return dest;
}
//

057. 문자열 복사하기(strcpy)057. 문자열 복사하기(strcpy)

Posted at 2011. 1. 30. 17:47 | Posted in Computer/초보자를 위한 C 언어 300제
#include <stdio.h>
#include <string.h>

#define KOREA "대한민국"

void main( void )
{
	char *string1;
	char string2[100];

	strcpy( string1, KOREA );		// 실행 시 에러 발생
	strcpy( string2, KOREA );
	strcpy( string2, "봄" );
}
//