강좌 : 부스트캠프 모두를 위한 컴퓨터과학(cs50 2019)
"문자열"이 아닌 '문자'로 printf
할 수도 있다
string names[4];
names[0] = "EMMA";
names[1] = "RODRIGO";
names[2] = "BRIAN";
names[3] = "DAVID";
printf(%s\n",names[0]);
// names[0][0] : names[0]의 0번째 값 = E
printf(%c%c%c%c\n",names[0][0],names[0][1],names[0][2],names[0][3]);
for(int i = 0; s[i] !='\0'; i++)
for(int i = 0, n=strlen(s); i<n; i++)
strlen()
은 문자열의 길이를 알려주는 함수로 string.h
라이브러리 안에 포함
!=
: 두 연산자의 값이 같지 않은지를 비교하는 연산자
#include<stdio.h>
#include<cs50.h>
#include<string.h>
int main(void)
{
string s = get_string("input: ");
printf("output: \n");
for(int i =0, n=strlen(s); i<n; i++) // for(int i = 0; s[i] !='\0'; i++)
{
printf("%c\n",s[i]);
}
}
printf("%c",s[i]-32);
printf("%c",toupper(s[i]));
toupper()
은 소문자를 대문자로 변환해주는 함수로 cytpe.h
라이브러리 안에 포함
// 대문자와 소문자의 ASCII값 차이로 하는 방법
#include<stdio.h>
#include<cs50.h>
#include<string.h>
int main(void)
{
string s = get_string("Before: "); // 사용자의 값을 s에 불러오고
printf("After: ");
for (int i = 0, n=strlen(s); i<n; i++) // 문자열의 길이만큼 반복
{
if(s[i]>='a'&& s[i]<='z') // 'a'보다크다 'z'보다 작으면
{
printf("%c",s[i]-32); // 32값을 뺀후 문자형태로 출력
}
else
{
printf("%c",s[i]);
}
}
printf("\n");
}
// toupper() 함수 사용하는 방법
#include<stdio.h>
#include<cs50.h>
#include<string.h>
#include<ctype.h>
int main(void)
{
string s = get_string("Before: ");
printf("After: ");
for (int i = 0, n=strlen(s); i<n; i++)
{
printf("%c",toupper(s[i]));
}
printf("\n");
}
#include<cs50.h>
#include<stdio.h>
int main(int argc, string argv[])
{
if(argc==2)
{
printf("hello, %s\n",argv[1]);
}
else
{
printf("hello, world\n");
}
}
argv[]
는 string배열이 된다(argc==2)
인 이유 : argv[0]
는 기본적으로 프로그램의 이름으로 저장된다 따라서 하나의 입력이 더 주어진다면 argv[1]
에 저장된다