[JW0004-3],[JW0004-4] 퀴즈-2(11장) 문제, 답

Post Reply
sungbum
Posts: 12
Joined: Sun Jan 04, 2015 10:06 pm

[JW0004-3],[JW0004-4] 퀴즈-2(11장) 문제, 답

Post by sungbum »

문제-1
2-1.PNG
2-1.PNG (34.13 KiB) Viewed 5526 times
답안-1

Code: Select all

  1 #include<stdio.h>
  2 #include<string.h>
  3
  4 int char_count(char *word[], int size);
  5
  6 int main(void)
  7 {
  8     int i=0;
  9     int j=0;
 10     char ch[10][20];
 11     char *word[10];
 12
 13     while((scanf("%s",&ch[i]))!= -1)
 14     {
 15         word[i]=ch[i];
 16         i++;
 17     }
 18     char_count(word,i);
 19
 20     return (0);
 21 }
 22
 23 int char_count(char *word[], int size)
 24 {
 25     int j=0;
 26     char *longest=word[j];
 27     char *shortest=word[j];
 28
 29     for(j=0;j<size;j++){
 30         if(strlen(longest)<strlen(word[j]))
 31             longest=word[j];
 32         else if(strlen(shortest)>=strlen(word[j]))
 33             shortest=word[j];
 34     }
 35     printf("입력 받은 영어 단어의 갯수: : %d \n", size);
 36     printf("입력 받은 영어 단어 중 가장 긴 단어: : %s \n", longest);
 37     printf("입력 받은 영어 단어 중 가장 짧은 단어: : %s \n", shortest);
 38
 39     return (0);
 40
 41 }
문제-2
2-2.PNG
2-2.PNG (31.63 KiB) Viewed 5526 times
답안-2

Code: Select all

  1 #include <stdio.h>
  2 #include <string.h>
  3
  4 int word_count(char *line, int size);
  5
  6 void main(void) {
  7     int i;
  8     int size=0;
  9     int small=0, capital=0;
 10     char str[50];
 11
 12     fgets(str, 50, stdin);
 13     *(str + strlen(str) -1) = '\0';
 14
 15     for(i=0 ; ; i++) {
 16         if(str[i] == '\0')
 17             break;
 18         size++;
 19     }
 20
 21     for(i=0; i<size; i++) {
 22         if(str[i]>= 'a' && str[i] <= 'z') {
 23             str[i] -= ('a'-'A');
 24             small ++;
 25         }
 26
 27         else if(str[i]>='A' && str[i] <= 'Z') {
 28             str[i] += ('a'-'A');
 29             capital++;
 30         }
 31         else if(str[i] == ' ') ;
 32
 33         else printf("Error\n");
 34     }
 35
 36     printf("변경된 문자열: %s\n", str);
 37     printf("문자열의 길이: %d\n", size);
 38     printf("단어의 수: %d\n", word_count(str, size));
 39     printf("변경된 소문자: %d\n", small);
 40     printf("변경된 대문자: %d\n", capital);
 41 }
 42
 43 int word_count(char *line, int size) {
 44     int i;
 45     int word=0;
 46
 47     for(i=0; i<size; i++)
 48         if(line[i] == ' ') word++;
 49
 50     return word+1;
 51 }


Post Reply