편집 요약 없음 |
편집 요약 없음 |
||
97번째 줄: | 97번째 줄: | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
// string mirror | |||
#include <stdio.h> | #include <stdio.h> | ||
#include <stdlib.h> | #include <stdlib.h> | ||
102번째 줄: | 103번째 줄: | ||
int main() | int main() | ||
{ | { | ||
int strlen = | int strlen = 1024; | ||
char *string = (char *)calloc(strlen, sizeof(char)); | char *string = (char *)calloc(strlen, sizeof(char)); | ||
gets(string); | |||
char *str = string + strlen - 1; | char *str = string + strlen - 1; | ||
while (str != (string - 1)) | while (str != (string - 1)) | ||
112번째 줄: | 113번째 줄: | ||
str--; | str--; | ||
} | } | ||
free(string); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
2021년 10월 26일 (화) 13:19 판
문자 입출력 함수
//함수의 매개변수는 포인터로 넣는다.
int fgetc( FILE *fp); //파일에서 문자를 입력받는다.
int fputc( int c, FILE *fp); //c에 저장된 문자를 파일로 출력한다.
문자열 파일에 출력 예제
fp = fopen("파일 이름.txt", "w");
fputc('a',fp);
fputc('b',fp);
fputc('c',fp);
fclose(fp);
실행 결과(파일)
abc
문자열 파일에서 읽어오기 예제
fp = fopen("sample.txt", "r");
while((c = fgetc(fp) ! = EOF)
putchar(c);
fclose(fp);
문자열 단위 입출력
윈도우에서 gcc 쓰는 법
mingw64 gcc 설치하기
MSYS2를 통해서 gcc 설치하기
- MSYS2에서 직접 코딩하기 pacman -S gcc
- MSYS2에서 GCC 설치해서 파워쉘이나 도스창에서 컴파일하기
WSL(Windows Subsystem Linux?) 설치해서 gcc이용하기
gcc 설치해서 비주얼 스튜디오 코드로 코딩하기
가상머신으로 리눅스 띄우고 쓰기
#include <stdio.h>
#include <string.h>
int main()
{
char str[128] = {0};
scanf("%s", str);
int strleng = strlen(str);
for (int i = strleng; i > 0 ; i--)
printf("%c", str[i]);
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[128] = {0};
scanf("%s", str);
int strleng = strlen(str);
for (int i = strleng; i > 0 ; i--)
printf("%c", str[i]);
}
// string mirror
#include <stdio.h>
#include <stdlib.h>
int main()
{
int strlen = 1024;
char *string = (char *)calloc(strlen, sizeof(char));
gets(string);
char *str = string + strlen - 1;
while (str != (string - 1))
{
if (*str != 0)
printf("%c", *str);
str--;
}
free(string);
}