이것저것
정올 - 문자열2 본문
http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&sca=10f0
JUNGOL
www.jungol.co.kr
자가진단1
#include <stdio.h>
int main(void) {
char str[5][30];
for (int i = 0; i < 5; i++)
scanf("%s", str[i]);
for (int i = 4; i >= 0; i--)
printf("%s\n", str[i]);
}
자가진단2
#include <stdio.h>
int main(void) {
char str[100];
int n = 1;
fgets(str, 100, stdin);
for (int i = 0; str[i] != '\0'; i++) {
if (n % 2 == 1) { // 홀수 번째 단어
printf("%c", str[i]);
}
if (str[i] == ' ') { // 공백 등장시
n++; // 단어 인덱스 증가
if(n % 2 == 1)
printf("\n");
}
}
}
자가진단3
#include <stdio.h>
#include <string.h>
int main(void) {
char str[10][100];
char c;
for (int i = 0; i < 10; i++)
scanf("%s", str[i]);
scanf(" %c", &c);
for (int i = 0; i < 10; i++) {
if (str[i][strlen(str[i]) - 1] == c)
printf("%s\n", str[i]);
}
}
자가진단4
#include <stdio.h>
#include <string.h>
int main(void) {
char str1[] = "Hong Gil Dong";
char str2[100];
strcpy(str2, str1);
printf("%s", str2);
}
자가진단5
#include <stdio.h>
#include <string.h>
int main(void) {
char str[20];
scanf("%s", str);
strcat(str, "fighting");
printf("%s", str);
}
자가진단6
#include <stdio.h>
#include <string.h>
int main(void) {
char str1[20], str2[20];
scanf("%s %s", str1, str2);
strncpy(str2, str1, 2); // str1의 앞부분 두자 str2 앞부분에 복사
strncat(str2, str1, 2); // str1의 앞부분 두자 str2 뒷부분에 복사
printf("%s", str2);
return 0;
}
자가진단7
#include <stdio.h>
#include <string.h>
int main(void) {
char str[100];
scanf("%s", str);
// 'c' 존재 여부 확인
if (strchr(str, 'c') == NULL)
printf("NO ");
else
printf("YES ");
// 문자열 'ab' 존재 여부 확인
if (strstr(str, "ab") == NULL)
printf("NO");
else
printf("YES");
return 0;
}
자가진단8
#include <stdio.h>
#include <string.h>
int main(void) {
char str1[10], str2[10], str3[10];
char re[10];
scanf("%s %s %s", str1, str2, str3);
strcpy(re, (strcmp(str1, str2) < 0 ? str1 : str2));
printf("%s", strcmp(re, str3) < 0 ? re : str3);
return 0;
}
자가진단9
#include <stdio.h>
#include <string.h>
int main(void) {
char str[5][20], temp[20];
for (int i = 0; i < 5; i++)
scanf("%s", str[i]);
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 5; j++) {
if (strcmp(str[i], str[j]) < 0) { // 비교
strcpy(temp, str[i]); // 자리바꾸기
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}
}
// 출력
for (int i = 0; i < 5; i++)
printf("%s\n", str[i]);
}
자가진단A
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char str[20];
fgets(str, 20, stdin);
int integer = atoi(str) * 2; // 정수로 변환해 2배
double doub = atof(str); // 실수로 변환
printf("%d\n%.2f", integer, doub);
}
자가진단B
#include <stdio.h>
#include <string.h>
int main(void) {
char str[5][100];
char temp[100];
for (int i = 0; i < 5; i++)
scanf("%s", str[i]);
// 합치기
for (int i = 1; i < 5; i++)
strcat(str[0], str[i]);
// 복붙
strcpy(temp, str);
// 3개씩 자르기
for (int i = 0; i < strlen(temp); i+=3)
printf("%c%c%c\n", temp[i], temp[i + 1], temp[i + 2]);
}
형성평가1
#include <stdio.h>
int main(void) {
char str[100];
char result[10][20];
int a = 0, b = 0;
fgets(str, 100, stdin);
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == ' ') { // 공백 등장시
result[a][b] = 0;
a++;
b = 0;
}
else { // 단어 저장
result[a][b] = str[i];
b++;
}
}
result[a][b - 1] = '\0'; // 마지막 설정
// 출력
for (int k = a; k >= 0; k--)
printf("%s\n", result[k]);
}
형성평가2
#include <stdio.h>
int main(void) {
char str[5][20] = {
"flower", "rose", "lily", "daffodil", "azalea"
};
char c;
int n = 0;
scanf("%c", &c);
for (int i = 0; i < 5; i++) {
if (str[i][1] == c || str[i][2] == c) {
printf("%s\n", str[i]);
n++;
}
}
printf("%d", n);
}
형성평가3
#include <stdio.h>
#include <string.h>
int main(void) {
char str[50][100];
int i = 0;
while (1) {
scanf("%s", str[i]);
if (strcmp(str[i], "0") == 0){
printf("%d\n", i);
for (int j = 0; j < i; j += 2)
printf("%s\n", str[j]);
break;
}
i++;
}
}
형성평가4
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
char str[10][100], temp[100];
for (int i = 0; i < n; i++)
scanf("%s", str[i]);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (strcmp(str[i], str[j]) > 0) {
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}
}
for(int i = 0; i<n; i++)
printf("%s\n", str[i]);
return 0;
}
형성평가5
#include <stdio.h>
#include <string.h>
int main(void) {
char str[5][100], c[2], string[100];
for (int i = 0; i < 5; i++)
scanf("%s", str[i]);
scanf(" %s", c);
scanf(" %s", string);
for (int i = 0; i < 5; i++) {
if (strstr(str[i], c) || strstr(str[i], string)) {
printf("%s\n", str[i]);
}
}
}
형성평가6
#include <stdio.h>
#include <string.h>
int main(void) {
char str1[100], str2[100];
int n;
scanf("%s %s %d", str1, str2, &n);
strcat(str1, str2); // 붙이기
strncpy(str2, str1, n);
printf("%s\n%s", str1, str2);
}
형성평가7
#include <stdio.h>
#include <string.h>
int main(void) {
char str1[100], str2[100];
scanf("%s %s", str1, str2);
printf("%d", atoi(str1) * atoi(str2));
return 0;
}
형성평가8
#include <stdio.h>
#include <string.h>
int main(void) {
char str[20];
while (1) {
scanf("%s", str);
if (strcmp(str, "END") == 0)
break;
for (int i = strlen(str) - 1; i >= 0; i--)
printf("%c", str[i]);
printf("\n");
}
return 0;
}
형성평가9
#include <stdio.h>
#include <string.h>
int main(void) {
char str[30], result[100];
int n;
double b;
scanf("%d %lf %s", &n, &b, str);
sprintf(result, "%d%.3lf%s", n, b, str);
for (int i = 0; result[i] != 0; i++) {
printf("%c", result[i]);
if (i == (int)((strlen(result) - 1) / 2))
printf("\n");
}
}