Basics/자기주도 C언어 프로그래밍

[Chapter] 14 문자열 I

whereareyoung 2023. 7. 14. 15:18

//1 문자 하나를 입력 받아서 그 문자의 아스키 코드를 출력하는 작업을 반복하다가 '0' 이 입력되면 아스키 코드를 출력하고 종료하는 프로그램을 작성하시오.
입력 예
a
B
1
c
0
 
출력 예 
a -> 97
B -> 66
1 -> 49
c -> 99
0 -> 48
 
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
 
int main()
{
char ch;
while (1)
{
scanf(" %c", &ch);
printf("%c -> %d\n", ch, ch);
 
if (ch == '0') break;
}
return 0;
}
 
 
 
 
자가진단 1 33부터 127 사이의 숫자를 계속 입력받아 입력받은 숫자의 아스키코드에 해당하는 문자를 출력하다가 범위를 벗어나는 입력이 들어오면 종료하는 프로그램을 작성하시오.
출력 예 
ASCII code =? 66
B
ASCII code =? 122
z
ASCII code =? 0
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
 
int main()
{
int ch;
while (1)
{
printf("ASCII code =? ");
scanf("%d", &ch);
 
if (ch < 1 || ch > 128) break;
printf("%c \n", ch, ch);
}
return 0;
}
 
 
 
//2 문자를 입력받다가 입력 없이 그냥 엔터만 치면 입력받은 문자를 모두 출력하는 프로그램을 작성하시오.
입력 예 ASDFghjk
출력 예 ASDFghjk
(소스1)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int len, i;
char str[50];
 
for (i = 0; i < 50; i++)
{
scanf("%c", &str[i]);
if (str[i] == 10) break;
}
 
len = i;
for (i = 0; i < len; i++)
{
printf("%c", str[i]);
}
printf("\n");
return 0;
}
(소스2)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
char str[50];
 
scanf("%s", str);
 
printf("%s \n",str);
 
return 0;
}
 
 
 
자가진단 2 문자열을 입력받은 뒤 그 문자열을 이어서 두 번 출력하는 프로그램을 작성하시오.
입력 예 ASDFG
출력 예 ASDFGASDFG
 
(소스1)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
 
int main()
{
int len, i, k;
char str[50];
 
for (i = 0; i < 50; i++)
{
scanf("%c", &str[i]);
if (str[i] == 10) break;
}
 
len = i;
 
for (k = 0; k < 2; k++)
{
for (i = 0; i < len; i++)
{
printf("%c", str[i]);
}
}
 
printf("\n");
return 0;
}
 
(소스2)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
char str[50];
 
scanf("%s", str);
 
printf("%s%s \n",str, str);
 
return 0;
}
 
 
 
//3 문자열을 선언하고 "jungol olympiad"로 초기화 한 후 0~14 사이의 정수 5개를 입력 받아 문자열에서 해당하는 위치의 문자를 차례로 출력하는 프로그램을 작성하시오. 문자열의 맨 앞의 위치는 0이다.
입력 예 10 13 2 14 1
출력 예 mandu
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int i;
int su[5];
char str[50] = "jungol olympiad";
 
for (i = 0; i < 5; i++)
{
scanf("%d", &su[i]);
}
 
for (i = 0; i < 5; i++)
{
printf("%c", str[su[i]]);
}
printf("\n");
 
return 0;
}
 
 
 
자가진단 3 문자열을 "Hong Gil Dong"으로 초기화 한 후 3번부터 6번까지의 문자를 차례로 출력하시오.
출력 예 g Gi
 
int main()
{
int i;
 
char str[50] = "Hong Gil Dong";
 
for (i = 3; i <= 6; i++)
{
printf("%c", str[i]);
}
 
printf("\n");
 
return 0;
}
 
 
 
//4 문자열을 입력받아서 문자열의 길이를 구하고 입력받은 역순으로 출력하는 프로그램을 작성하시오.
입력 예 Jungol
출력 예 
입력받은 문자열의 길이는 6입니다.
lognuJ
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
 
int main()
{
int len, i;
char str[50];
 
scanf("%s", str);
 
len = strlen(str);
 
printf("입력받은 문자열의 길이는 %d입니다. \n", len);
for (i = len - 1; i >= 0; i--)
{
printf("%c", str[i]);
}
printf("\n");
 
return 0;
}
 
 
 
자가진단 4 문자열을 입력받고 정수를 입력 받아서 문자열의 맨 뒤부터 정수만큼 출력하는 프로그램을 작성하시오. 만약 입력받은 정수가 문자열의 길이보다 크다면 모두 출력한다.
입력 예 korea 3
출력 예 aer
 
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
 
int main()
{
int len, i;
int a;
char str[50];
 
scanf("%s", str);
scanf("%d", &a);
 
if (a < 0) return 0;
 
len = strlen(str);
 
for (i = len - 1; i >= len - a; i--)
{
if (a > len) a = len;
printf("%c", str[i]);
}
 
printf("\n");
return 0;
}
 
 
 
//5 다음과 같이 "우리나라 대한민국!"을 문자형 배열에 초기화하고 길이를 구하여 다음과 같이 출력하는 프로그램을 작성하시오.
출력 예 
우리나라 대한민국!
위 문자열의 길이는 ??입니다.
 
#include <stdio.h>
#include <string.h>
 
int main()
{
int len;
char str[50] = "우리나라 대한민국!";
len = strlen(str);
 
printf("%s\n", str);
printf("위 문자열의 길이는 %d입니다. \n", len);
return 0;
}
 
 
 
자가진단 5 두 개의 문자열을 입력받아서 두 문자열의 길이의 합을 출력하는 프로그램을 작성하시오.
입력 예
Korean
English
출력 예 13
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
 
int main()
{
int len_1, len_2;
char str_1[50], str_2[50];
 
scanf("%s", str_1);
scanf("%s", str_2);
 
len_1 = strlen(str_1);
len_2 = strlen(str_2);
 
printf("%d", len_1 + len_2);
return 0;
}
 
 
 
//6 문자를 입력받아 어떤 종류인지 구분하여 출력하는 작업을 반복하다가 영문자와 숫자 이외의 문자가 입력되면 종료하는 프로그램을 작성하시오.
입출력 예 
a
소문자입니다.
B
대문자입니다.
5
숫자문자입니다.
#
영문, 숫자 이외의 문자입니다.
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
 
int main()
{
char ch;
while (1)
{
ch = getchar();
getchar();
 
if (isupper(ch))
{
printf("대문자입니다. \n");
}
else if (islower(ch))
{
printf("소문자입니다. \n");
}
else if (isdigit(ch))
{
printf("숫자문자입니다. \n");
}
else
{
printf("영문, 숫자 이외의 문자입니다. \n");
break;
}
}
return 0;
}
 
 
 
자가진단 6 문자를 입력받아 알파벳 문자인 경우에는 그대로 출력하고 숫자인 경우는 아스키코드값을 출력하는 작업을 반복하다가 기타의 문자가 입력되면 종료하는 프로그램을 작성하시오.
입출력 예
A
A
1
49
@
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
 
int main()
{
char ch;
 
while (1)
{
ch = getchar();
getchar();
 
if (isupper(ch))
{
printf("%c \n", ch);
}
else if (islower(ch))
{
printf("%c \n", ch);
}
else if (isdigit(ch))
{
printf("%d \n", ch);
}
else break;
}
return 0;
}
 
 
 
//7 100글자 이하의 문자열을 입력받아 대문자는 소문자로, 소문자는 대문자로 변환하여 출력하는 프로그램을 작성하시오.
입력 예 ILoveYou.
출력 예 ilOVEyOU.
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
 
int main()
{
int i, len;
char st[101];
 
scanf("%s", st);
len = strlen(st);
 
for (i = 0; i < len; i++)
{
if (isupper(st[i]))
{
st[i] = tolower(st[i]);
}
else if (islower(st[i]))
{
st[i] = toupper(st[i]);
}
}
printf("%s \n", st);
return 0;
}
 
 
 
자가진단 7 문자열을 입력받아 알파벳 문자만 모두 대문자로 출력하는 프로그램을 작성하시오. 입력 예 1988-Seoul-Olympic!!!
출력 예 SEOULOLYMPIC
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
 
int main()
{
int i, len;
char st[101];
 
scanf("%s", st);
 
len = strlen(st);
 
for (i = 0; i < len; i++)
{
if (islower(st[i]))
{
st[i] = toupper(st[i]);
}
}
 
for (i = 0; i < len; i++)
{
if (isupper(st[i]))
{
printf("%c", st[i]);
}
}
return 0;
}
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
 
int main()
{
int i, len;
char st[101];
 
scanf("%s", st);
 
len = strlen(st);
 
for (i = 0; i < len; i++)
{
if (isalpha(st[i]))
{
printf("%c", toupper(st[i]));
}
}
 
return 0;
}
 
 
 
//8 공백을 포함하여 100글자 이하의 문자열(문장)을 입력받아 공백을 기준으로 줄을 바꾸어 출력하는 프로그램을 작성하시오.
입력 예 I Love Korea
출력 예 
I
Love
Korea
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
 
int main()
{
int i, len;
char st[101];
 
fgets(st, 101, stdin);
len = strlen(st);
while (st[len - 1] == '/n' || st[len - 1] == '/r')
st[--len] = '/0';
 
for (i = 0; i < len; i++)
{
if (st[i] == ' ')
{
printf("\n");
}
else
{
printf("%c", st[i]);
}
}
printf("\n");
return 0;
}
 
 
 
자가진단 8 공백을 포함한 100글자 이하의 문자열을 입력받아 문장을 이루는 단어의 개수를 출력하는 프로그램을 작성하시오.
입력 예 My name is Kimchulsoo
출력 예 4
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
 
int main()
{
int i, len;
char st[101];
int cnt = 1;
 
fgets(st, 101, stdin);
len = strlen(st);
while (st[len - 1] == '/n' || st[len - 1] == '/r')
st[--len] = '/0';
 
for (i = 0; i < len; i++)
{
if (st[i] == ' ') cnt++;
}
 
printf("%d", cnt);
return 0;
}
 
(간단버전)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
 
int main()
{
int i, len;
char st[101];
int cnt = 1;
 
scanf("%[^\n]", &st);
len = strlen(st);
 
for (i = 0; i < len; i++)
{
if (st[i] == ' ')
{
cnt++;
}
}
 
printf("%d", cnt);
return 0;
}
 
 
 
 
//9 49자 이하의 문자열을 입력받아서 문자수만큼 왼쪽으로 한 바퀴 회전하여 출력하는 프로그램을 작성하시오.
입력 예 ABCDE12345
출력 예 
BCDE12345A
CDE12345AB
DE12345ABC
E12345ABCD
12345ABCDE
2345ABCDE1
345ABCDE12
45ABCDE123
5ABCDE1234
ABCDE12345
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
 
int main()
{
int i, j, len;
char word[50], tmp;
 
scanf("%s", word);
 
len = strlen(word);
 
for (i = 0; i < len; i++)
{
tmp = word[0];
for (j = 0; j < len - 1; j++)
{
word[j] = word[j + 1];
}
word[len - 1] = tmp;
printf("%s \n", word);
}
return 0;
}
 
 
 
 
 
자가진단 9 문자열을 입력 받아서 문자수만큼 오른쪽으로 한 바퀴 회전하여 출력하는 프로그램을 작성하시오. 입력받는 문자열의 길이는 100자 이하이다.
입력 예 PROGRAM
출력 예 
MPROGRA
AMPROGR
RAMPROG
GRAMPRO
OGRAMPR
ROGRAMP
PROGRAM
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
 
int main()
{
int i, j, len;
char word[50], tmp;
 
scanf("%s", word);
 
len = strlen(word);
 
for (i = 0; i < len; i++)
{
tmp = word[len - 1];
for (j = len - 1; j >= 1; j--)
{
word[j] = word[j - 1];
}
word[0] = tmp;
 
printf("%s \n", word);
}
 
return 0;
}
 
 
 
 

'Basics > 자기주도 C언어 프로그래밍' 카테고리의 다른 글

[Chapter] 16 구조체  (0) 2023.07.14
[Chapter] 15 문자열 II  (0) 2023.07.14
[Chapter] 13 함수 III  (0) 2023.07.14
[Chapter] 12 함수 II  (0) 2023.07.14
[Chapter] 11 함수 I  (0) 2023.07.14