사용자:하늘/메모장/C

삭제는 사용자들의 토론에 따라 결정됩니다.
다른 의견이 있으면 틀을 {{삭제보류}}로 바꿔 달아주시고, 토론 문서에서 문서의 삭제에 관해 토론해주세요.



문자 입출력 함수

void reverse(int *a, int n) {
    for (int i = 0; i < n/2; i++) {
        int t = a[i]; a[i] = a[n-1-i]; a[n-1-i] = t;
    }
}

시간복잡도 O(n/2)

극단적으로 짧은 코드

void reverse(int *a, int n) {
    int *end = a + n - 1;
    while ( (a != end && a != end+1)) {
        int t = *a; *a = *end; *end = t;
        a++, end--;
    }
}

시간복잡도 O(n/2)

//함수의 매개변수는 포인터로 넣는다.

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[1024] = {0};
    gets(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);
}

동적 할당은 힙, 정적은 스택 힙은 실행 중에 할당, 스택은 실행 전 컴파일 때 이미 크기가 결정되어 있음. 컴파일 타임 때 크기를 결정하므로 배열의 크기를 변수로 놓을 수 없다. 동적 할당은 런타임에 크기가 결정되므로 배열 크기를 변수로 놓는 것이 가능하다.

#include <stdio.h>
#include <string.h>

void delchars (char *str, char ch)
{
    int len = strlen(str);
    int i = 0;
    while ( i < len )    
    {
        if (str[i] == ch)
        {
           int j = i, width = 0;
            if ( str[i+1] == ch)
                width = 2;
            else
                width = 1;
            while (j < len)
                str[j] = str[j+width], j++;
        }
        i++;
    }
}
int main ( )
{
    char s1[128];
    strcpy(s1, "HelloWorld");
    delchars(s1, 'l');
    printf("[%s]\n", s1); // [HeoWord]
    delchars(s1, 'W');
    printf("[%s]\n", s1); // [Heoord]
}
#include <stdio.h>
#include <string.h>
int findStr (char *str, char *target)
{
    int i = 0;
    int j = 0;
    for ( ; (*target) != '\0' ; i++)
    {
        int count = 0;
        for ( ; (*str) != '\0'; j++)
        {
            if ( *str == *target)
                count++;      
            str++; 
        }
        str = str - j;
        printf("'%c' %d개", *target, count);
        target++;
    }
    printf("\n");
}
int main () {
    char s[128];
    strcpy (s, "Hello,World");
    findStr(s, "Ho");
    findStr(s, "ld");
    findStr(s, "lod");
}

ssh 루트 사용자 로그인 금지, 키없이 로그인 금지, ssh 키만 이용하도록 하기, 로그인 실패 횟수에 따라 감옥에 보내기

키 없으면 못 풀어 ㅋㅋ

Brute force공격(무작위 사전 아이디 대입공격)