Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

새싹교실/2017/꽃밭/0523: Difference between revisions

From ZeroWiki
No edit summary
No edit summary
Line 114: Line 114:
return 0;
return 0;
}
}
== 혜민 ==
== 현지 ==
  #include<stdlib.h>
  #include<stdlib.h>
  #include<time.h>
  #include<time.h>
Line 125: Line 125:
  {
  {
  char input[7][10] = { "red", "blue", "orange", "green", "black", "white", "pink" };
  char input[7][10] = { "red", "blue", "orange", "green", "black", "white", "pink" };
   
  char* comp = "Computer", *user = "User";
  char* comp = "Computer", *user = "User";
   
   
  srand(time(NULL));
  srand(time(NULL));
  int random = rand() % 7;    //컴퓨터가 좋아하는 색깔 생성하기
  int random = rand() % 7;    //컴퓨터가 좋아하는 색깔 생성하기
  char* computerColor = input[random];   //computerColor : 컴퓨터가 좋아하는 색깔
  char* computerColor = input[random];//computerColor : 컴퓨터가 좋아하는 색깔
   
   
  printf("%10s : what is your favorite color? ", comp);
  printf("%10s : what is your favorite color? ", comp);
  char str1[30], str2[30]="My favorite color is ";
  strcpy(str1, str2);
  char str1[100];
char mycolor[10];
char str2[100];
strcpy(str1, "My favorite color is");
  gets(str2);
  //문자열을 선언해서, strcpy로 "My favorite color is "로 초기화합니다.
  //문자열을 선언해서, strcpy로 "My favorite color is "로 초기화합니다.
  gets(mycolor);//gets로 내가 좋아하는 색을 입력받고
  //gets로 내가 좋아하는 색을 입력받고
strcat(str1, mycolor);//strcat을 써서 "My favorite color is <색깔이름>"이라는 문자열을 만듭니다.
//strcat을 써서 "My favorite color is <색깔이름>"이라는 문자열을 만듭니다.
  printf("%10s : %s", user, str1);
  printf("%10s : %s", user,strcat(str1,str2));
  //완성된 문자열을 여기서 출력해주세요.
  //완성된 문자열을 여기서 출력해주세요.
   
   
   
   
  if (strcmp(computerColor, mycolor)==0/* 만약에 컴퓨터와 내가 좋아하는 색깔이 같다면(strcmp를 사용) */) {
  if (strcmp(computerColor,str2)==0) {
  printf("%10s : We like same color!\n", comp);
  printf("%10s : We like same color!\n", comp);
   
   

Revision as of 07:54, 23 May 2017

◀이전수업▒▒▒▒▒다음수업▶ 새싹교실/2017 새싹교실/2017/꽃밭

실습

실습 1 : 대문자로 바꾸는 함수

  • 코드
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

void toUpper(char* str);

int main()
{
	char input[100];

	printf("\n 문자열을 입력하세요(영문자로만 공백없이) : ");
	gets(input);

	toUpper(input);

	printf("\n 변환된 문자열 : ");
	puts(input);

	return 0;
}

//알파벳 소문자를 대문자로 변환하는 함수
void toUpper(char* str){

}

실습 2 : 내가 좋아하는 색깔은?

#include "header.h"
#ifdef String_Quiz
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>


int main()
{
	char input[7][10] = { "red", "blue", "orange", "green", "black", "white", "pink" };
        char* computerColor = input[random];    //computerColor : 컴퓨터가 좋아하는 색깔
	char* comp = "Computer", *user = "User";

	srand(time(NULL));
	int random = rand() % 7;     //컴퓨터가 좋아하는 색깔 생성하기

	printf("%10s : what is your favorite color? ", comp);
	//문자열을 선언해서, strcpy로 "My favorite color is "로 초기화합니다.
        //gets로 내가 좋아하는 색을 입력받고
        //strcat을 써서 "My favorite color is <색깔이름>"이라는 문자열을 만듭니다.
	printf("%10s : ", user);
	//완성된 문자열을 여기서 출력해주세요.


	if (/* 만약에 컴퓨터와 내가 좋아하는 색깔이 같다면(strcmp를 사용) */){
		printf("%10s : We like same color!\n", comp);

	}
	else{
		printf("%10s : I don't like that color. I like %s\n", comp, computerColor);
	}


	return 0;
}

과제 소개

과제 제출

현지

  1. include<stdlib.h>
  2. include<time.h>
  3. define _CRT_SECURE_NO_WARNINGS
  4. include <stdio.h>
  5. include <string.h>


int main() { char input[7][10] = { "red", "blue", "orange", "green", "black", "white", "pink" };

char* comp = "Computer", *user = "User";

srand(time(NULL)); int random = rand() % 7; //컴퓨터가 좋아하는 색깔 생성하기 char* computerColor = input[random];//computerColor : 컴퓨터가 좋아하는 색깔

printf("%10s : what is your favorite color? ", comp);

char str1[100]; char str2[100]; strcpy(str1, "My favorite color is"); gets(str2); //문자열을 선언해서, strcpy로 "My favorite color is "로 초기화합니다. //gets로 내가 좋아하는 색을 입력받고 //strcat을 써서 "My favorite color is <색깔이름>"이라는 문자열을 만듭니다. printf("%10s : %s", user,strcat(str1,str2)); //완성된 문자열을 여기서 출력해주세요.


if (strcmp(computerColor,str2)==0) { printf("%10s : We like same color!\n", comp);

} else { printf("%10s : I don't like that color. I like %s\n", comp, computerColor); }


return 0; }

현지

#include<stdlib.h>
#include<time.h>
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>


int main()
{
	char input[7][10] = { "red", "blue", "orange", "green", "black", "white", "pink" };
	    
	char* comp = "Computer", *user = "User";

	srand(time(NULL));
	int random = rand() % 7;     //컴퓨터가 좋아하는 색깔 생성하기
	char* computerColor = input[random];//computerColor : 컴퓨터가 좋아하는 색깔

	printf("%10s : what is your favorite color? ", comp);
	
	char str1[100];
	char str2[100];
	strcpy(str1, "My favorite color is");
	gets(str2);
	//문자열을 선언해서, strcpy로 "My favorite color is "로 초기화합니다.
	//gets로 내가 좋아하는 색을 입력받고
	//strcat을 써서 "My favorite color is <색깔이름>"이라는 문자열을 만듭니다.
	printf("%10s : %s", user,strcat(str1,str2));
	//완성된 문자열을 여기서 출력해주세요.


	if (strcmp(computerColor,str2)==0) {
		printf("%10s : We like same color!\n", comp);

	}
	else {
		printf("%10s : I don't like that color. I like %s\n", comp, computerColor);
	}


	return 0;
}

유진

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main()
{
	char input[7][10] = { "red", "blue", "orange", "green", "black", "white", "pink" };
	
	char* comp = "Computer", *user = "User";

	srand(time(NULL));
	int random = rand() % 7;     //컴퓨터가 좋아하는 색깔 생성하기
	char* computerColor = input[random];    //computerColor : 컴퓨터가 좋아하는 색깔
	printf("%10s : what is your favorite color? \n", comp);
	//문자열을 선언해서, strcpy로 "My favorite color is "로 초기화합니다.
	char str[50];
	strcpy(str, "My favorite color is ");
	//gets로 내가 좋아하는 색을 입력받고
	char color[10];
	gets(color);
	//strcat을 써서 "My favorite color is <색깔이름>"이라는 문자열을 만듭니다.
	strcat(str, color);
	printf("%10s : ", user);
	//완성된 문자열을 여기서 출력해주세요.
	printf("%s\n", str);

	if (/* 만약에 컴퓨터와 내가 좋아하는 색깔이 같다면(strcmp를 사용) */strcmp(computerColor,color)==0)
	{
		printf("%10s : We like same color!\n", comp);

	}
	else{
		printf("%10s : I don't like that color. I like %s\n", comp, computerColor);
	}


	return 0;
}

후기

물주는사람

유진

전에 문자열 부분은 거의 알지 못했는데, 이번에 문자열에 대해 배울 수 있어서 다음 프로그래밍 실습 시간에는 스스로 할 수 있을 것 같다. 문자열 함수가 다양하고 많은 기능을 갖고 있다는 것을 알 수 있었다.

현지

혜민

많이 쓰이는 함수 strlen, strcpy, strcat, strcmp의 사용법을 알게 되었고 사용해서 실습도 해보니 더욱 이해가 잘되었다. 스페이스 공간을 다른 문자로, 영문자를 대문자로 바꾸는 방법도 알게되어 유익했다.


◀이전수업▒▒▒▒▒다음수업▶ 새싹교실/2017 새싹교실/2017/꽃밭