C언어 비트단기 6일차(문자열 함수, 구조체)

=================================================================================

문자열의 길이 구하는 프로그램


#include <stdio.h>

#define size 255


int main(void)

{

char _in[size];

char _search[size];

char *p;

char *q;

int i = 0;

int count1 = 0;

int count2 = 0;

int count3 = 0;

int pos = 0;


printf("문자열 _in을 입력하세요 :\n");

scanf_s("%s",_in,sizeof(_in));


p = _in;

count1 = 0;

for(i = 0 ; i < size; i++)

{

if( *(p+i) == NULL)

{

break;

}

count1++;

}


printf("문자열 _search을 입력하세요 :\n");

scanf_s("%s",_search,sizeof(_search));


q = _search;

count2 = 0;


for(i = 0 ; i < size; i++)

{

if(*(q+i) == NULL)

{

break;

}

count2++;

}


p = _in;

q = _search;

count3 = 0;

pos = 0;


for(i = 0 ; i < count1; i++)

{

if(*(p+i) == *(q+pos))

{

if(pos == count2-1)

{

count3++;

}

else

{

pos++;

}

}

else

{

pos = 0;

}

}


printf("_in 문자열의 길이 = %d 입니다.\n",count1);

printf("_search 문자열의 길이 = %d 입니다.\n",count2);

printf("_in과 _search 문자열중에 같은 문자열의 개수 = %d개 입니다.\n",count3);


return 0;

}

=================================================================================

//구조체란?

#include <stdio.h>

#include <string.h>


struct Student // 구조체 Student <-- 태그명

{

int num;

char name[20];

int iq;

};

void ViewStu(struct Student *stu); //구조체에서는 언제나 포인터 형식

void StudyStu(struct Student *stu);

int main(void)

{

struct Student stu;

struct Student stu2 = {34, "홍길동"}; // 구조체 초기화

struct Student stu3 = {3}; //구조체 초기화


stu.num = 90; 

stu.iq = 100;

strcpy_s(stu.name, sizeof(stu.name),"강감찬"); //문자열 복사

printf(" 번호 : %d 이름 :%s\n",stu.num, stu.name);

ViewStu(&stu);

StudyStu(&stu);

printf(" 번호 : %d 이름 :%s 아이큐 : %d\n",stu.num, stu.name,stu.iq);

return 0;

}


void ViewStu(struct Student *stu)

{

printf(" 번호 : %d 이름 :%s\n",stu->num, stu->name);

}


void StudyStu(struct Student *stu)

{

stu->iq++;

printf(" 번호 : %d 이름 :%s 아이큐 : %d\n",stu->num, stu->name,stu->iq);

}

=================================================================================

//Alignment 배정

#pragma pack(1) // 1바이트 Alignment  지정 1, 2, 4, 8 중에 하나 입력 해주면 된다.

#include <stdio.h>


struct Foo

{

int a;

char b;

int c;

char d;

};


struct Soo

{

int a;

int c;

char b;

char d; //같은 형식 연속적으로 할당하는것이 메모리 할당 절약.

};


int main(void)

{

printf("%d %d\n",sizeof(struct Foo),sizeof(struct Soo));

return 0;

}

=================================================================================


//구조체 비트 필드

#include <stdio.h>


struct Flag

{

unsigned char grade:3; // 0,1,2,3,4,5,6 (: 3 => 3비트 배정해줘요);

unsigned char gender:1;//0:Female, 1 : Male

unsigned char married:1;//0:Is not married, 1: is Married

};

int main(void)

{

struct Flag flag;

flag.grade = 3;

flag.gender = 1;

flag.married = 1;


printf("%d %d %d\n",flag.grade, flag.gender, flag.married);

printf("%d\n",sizeof(flag));

return 0;

}

=================================================================================

//공용체란 ? 

#include <stdio.h>

#include <string.h>


union Foo //공용체는 동시에 한가지 데이터만 사용할수 있다.

{

int a;

char b;

char name[8];

int c;

float d;

};


int main(void)

{

union Foo foo;

foo.a = 8;

foo.c = 9;

strcpy_s(foo.name, sizeof(foo.name),"하이");

printf("a : %d c : %d name : %s\n",foo.a,foo.c,foo.name);

printf("size : %d \n",sizeof(foo));


return 0;

}


=================================================================================


//열거형이란? C언어에서는 열거형을 정수형으로 취급한다.

#include <stdio.h>

#include <string.h>


#define MAX_STU 50


enum BloodType

{

B_A, B_B, B_AB, B_O // 0,1,2,3 B_B = 9 라고도 정할수 있다.

};


enum SubType

{

KOREAN, MATH, ENGLISH, MAX_SUBJECT

};


const char *sub_title[MAX_SUBJECT] = {"국어", "수학","영어"};


//#define BLOOD_A    1

//#define BLOOD_B     2

//#define BLOOD_AB  3

//#define BLOOD_O    0


#define NOT_MAR 0

#define MARRIED 1


#define NOCAR 0

#define HASCAR 1


int main(void)

{

int scores[MAX_SUBJECT] = {-1,-1,-1};

int si = 0;

scores[KOREAN] = 80;

scores[ENGLISH] = 98;

scores[MATH] = 20;

for(si = 0; si < MAX_SUBJECT; si++)

{

printf("%s : %d\n",sub_title[si], scores[si]);

}


/*enum BloodType b;

b = B_A;

printf("%d\n",b);*/

return 0;

}

=================================================================================

//배열과 포인터를 이용한 문자열 사용

#include <stdio.h>

int main(void)

{

char name1[6] = "hello";

char name2[6] = "hello";

const char *str1 = "yahoo";

const char *str2 = "yahoo";


printf("name1 : %p name2 : %p \n",name1, name2);

printf("str1 : %p str2 : %p\n",str1, str2);


name1[0] = 'y';

//str1[0] = 'k';

printf("name1 : %s name2 : %s\n",name1, name2);

printf("str1 : %s str2 : %s\n",str1, str2);


return 0;

}

=================================================================================

//배열과 포인터를 이용한 문자열 사용

#include <stdio.h>

#include <string.h>


int main(void)

{

char name[10] = "";

strcpy(name, "hello");

printf("이름 : %s\n",name);

printf("문자열 길이 : %d\n",strlen(name));

if(strcmp(name, "hello") == 0)

{

printf("차이가 없다.\n");

}

else

{

printf("차이가 있다.\n");

}

return 0;

}

=================================================================================

//char 형식 배열에 문자열 초기화

#include <stdio.h>

#define MAX_NAME_LEN 50

#define MAX_ADDR_LEN 100


int main(void)

{

char name[MAX_NAME_LEN+1] = {'a','b','c'};

char addr[MAX_ADDR_LEN+1] = "제주도 제주시 애월읍 고내리";

printf("이름 : %s \n",name);

printf("주소 : %s \n",addr);


return 0;

}

=================================================================================

//strlen 함수 사용  예

#include <stdio.h>

#include <string.h>


#define MAX_NAME_LEN 50


int main(void)

{

char name[MAX_NAME_LEN+1]  = "hello";

printf("%s 길이 : %d \n",name,strlen(name));


return 0;

}

=================================================================================

//유니코드와 ASCII코드

#include <locale.h>

#include <stdio.h>

#include <string.h>


int main(void)

{

char c = 'a';

wchar_t wc = L'홍';

char name[10] = "홍길동";

wchar_t wname[10] = L"홍길동";

setlocale(LC_ALL,"korean"); //로케일 설정(지역 설정)

printf("c : %c wc : %lc\n",c,wc);

printf("name : %s wnmae %ls\n",name,wname);

printf("name 길이 : %d wname 길이 : %d\n",strlen(name),wcslen(wname));


return 0;

}

=================================================================================

//비교연산으로 문자열을 비교했을때의 버그

#include <stdio.h>

#include <string.h>

#define MAX_NAME_LEN 50


int main(void)

{

char name1[MAX_NAME_LEN+1] = "hello";

char name2[MAX_NAME_LEN+1] = "hello";


if(name1 == name2)

{

printf("%s 와 %s는 같다 \n",name1, name2);

}

else

{

printf("%s와 %s는 다르다.\n",name1, name2);

}

return 0;

}

=================================================================================

//문자열 비교

#include <stdio.h>

#include <string.h>

#define MAX_NAME_LEN 50


int main(void)

{

char name1[MAX_NAME_LEN+1] = "hello";

char name2[MAX_NAME_LEN+1] = "hello";

if(strcmp(name1, name2) == 0)

{

printf("%s 와 %s는 같다.\n",name1, name2);

}

else

{

printf("%s 와 %s는 다르다.\n",name1, name2);

}

return 0;

}

=================================================================================

//부분 문자열 비교

#include <stdio.h>

#include <string.h>

#define MAX_NAME_LEN 50


int main(void)

{

char name1[MAX_NAME_LEN+1] = "hello";

char name2[MAX_NAME_LEN+1] = "hello word";


if(strcmp(name1, name2) == 0)

{

printf("%s 와 %s는 같다.\n",name1,name2);

}

else

{

printf("%s 와 %s는 다르다.\n",name1, name2);

}

if(strncmp(name1, name2, 5) == 0)

{

printf("%s 와 %s의 %d개의 문자는 같다.\n",name1, name2, 5);

}

else

{

printf("%s 와 %s의 %d 개의 문자는 다르다.\n",name1, name2, 5);

}

return 0;

}

=================================================================================

//문자열 복사

#include <stdio.h>

#include <string.h>

#define MAX_NAME_LEN 50


int main(void)

{

char name1[MAX_NAME_LEN+1] = "hello";

char name2[MAX_NAME_LEN+1] = "";

char name3[MAX_NAME_LEN+1] = "";


strcpy_s(name2, sizeof(name2), name1);

printf("%s\n",name2);

strncpy_s(name3, sizeof(name3),name1,3);

printf("%s \n",name3);


return 0;

}

=================================================================================

문자열의 길이 구하는 프로그램



//program.c

#include "string.h"

#include <stdio.h>

#include <assert.h>


#define MAX_NAME_LEN 50


void TestStringLen();


int main(void)

{

char _in[MAX_NAME_LEN+1] = "";


printf("문자열을 입력하세요 :\n");

scanf_s(" %s",_in,sizeof(_in));


printf("%s 의 문자열의 길이 = %d입니다.\n",_in,StringLen(_in));

TestStringLen();

return 0;

}


void TestStringLen()

{

char arr[MAX_NAME_LEN+1] = "hello";


printf("Test StringLen Function \n");

assert(StringLen(arr) == 5);

printf("Success StringLen Function \n");

}





//string.c

#include "string.h";

#include <stdio.h>

#include <assert.h>


#define MAX_NAME_LEN 50


int StringLen(char *p)

{

int i = 0;

int count = 0;

char *q = 0;

q = p;

for(i = 0 ; i < MAX_NAME_LEN; i++)

{

if(*(q+i) == NULL)

{

break;

}

count++;

}

return count;

}





//string.h

#pragma once

//문자열 길이를 구하는 함수를 만드시오. - StringLen

int StringLen(char *p);



=================================================================================

문자열 비교하는 함수를 만드시오.


//Program.c

#include <stdio.h>

#include "string.h"

#include <assert.h>


#define MAX_NAME_LEN 255


void TestStringCompare();


int main(void)

{

char name1[MAX_NAME_LEN+1] = "";

char name2[MAX_NAME_LEN+1] = "";


printf("name1 을 입력하세요 :\n");

scanf_s(" %s",name1,sizeof(name1));

printf("name2 을 입력하세요 :\n");

scanf_s(" %s",name2,sizeof(name2));


if(StringCompare(name1, name2) == 0)

{

printf("%s와 %s는 같다. \n",name1, name2);

}

else

{

printf("%s 와 %s는 다르다.\n",name1, name2);

}

TestStringCompare();

printf("\n");

return 0;

}


void TestStringCompare()

{

char name1[MAX_NAME_LEN+1] = "hello";

char name2[MAX_NAME_LEN+1] = "hello";

char name3[MAX_NAME_LEN+1] = "Hello";


printf("Test StringCompare Function \n");

assert(StringCompare(name1,name2) == 0);

assert(StringCompare(name1,name3) == 1);

assert(StringCompare(name2,name3) == 1);

printf("Success StringCompare Function \n");

}




//string.c

#include <stdio.h>

#include "fun.h"

#include <assert.h>

#include <string.h>


int StringCompare(char *p1, char *p2)

{

char *q1 = "";

char *q2 = "";

int pos = 0;

int count1 = 0;

int count2 = 0;

int count3 = 0;

int i = 0;


q1 = p1;

q2 = p2;


count1 = strlen(q1);

count2 = strlen(q2);


if(count1 < count2)

{

for(i = 0 ; i < count1; i++)

{

if(*(q1+i) == *(q2+pos))

{

if(pos == count2-1)

{

return 0;

}

else

{

pos++;

}

}

else

{

pos = 0;

}

}

}

else

{

for(i = 0 ; i < count2; i++)

{

if(*(q1+i) == *(q2+pos))

{

if(pos == count1-1)

{

return 0;

}

else

{

pos++;

}

}

else

{

pos = 0;

}

}

}

return 1;

}





//string.h

#pragma once

//문자열 비교하는 함수를 만드시오. - StringCompare

int StringCompare(char *p1, char *p2);



=================================================================================


//Program.c

#include <stdio.h>

#include <string.h>

#include <assert.h>

#include "string.h"


#define MAX_NAME_LEN 255


void TestStringCopy();


int main(void)

{

char name1[MAX_NAME_LEN+1] = "";

char name2[MAX_NAME_LEN+1] = "";

char name3[MAX_NAME_LEN+1] = "";


printf("name1을 입력하세요 :\n");

scanf_s(" %s",name1,sizeof(name1));


printf("name2 의 값은 %s입니다.\n",

StringCopy(name2,sizeof(name2),name1));


TestStringCopy();

printf("\n");

}


void TestStringCopy()

{

char name1[MAX_NAME_LEN+1] = "hello";

char name2[MAX_NAME_LEN+1] = "";

char name3[MAX_NAME_LEN+1] = "";


printf("Test StringCopy Function \n");

assert(StringCopy(name2,sizeof(name2),name1) == "hello");

assert(StringCopy(name3,sizeof(name3),name1) == "hello");

printf("Success StringCopy Function \n");

}




//string.c

#include <stdio.h>

#include <assert.h>

#include <string.h>


#define MAX_NAME_LEN 255


char *StringCopy(char *p1,int size, char *p2)

{

char *q1 = "";

char *q2 = "";

int i = 0;


for(i = 0; i < MAX_NAME_LEN; i++)

{

*(q1+i) = *(q2+i);

}


return q1;

}




//string.h

#pragma once

//문자열 복사하는 함수를 만드시오. - StringCopy


char *StringCopy(char *p1,int size, char *p2);







728x90

이 글을 공유하기

댓글

Designed by JB FACTORY