편집 기록

편집 기록
  • 프로필 nowp님의 편집
    날짜2021.12.01

    C언어 전화번호 하이픈 추가하는 코딩 조언 부탁드립니다


    #include <stdio.h>
    
    typedef struct people{
      char name[100];
      int num;
      char phone[16];
    }People;
    
    void phone_number(char m[], int n){
      char phone_a[16];
      int i;
      for(i = 0; i <= 3; i++){
        phone_a[i] = m[i];
        if(i == 3)
          phone_a[3] = n;
      }
    
      for(i = 4; i <= 8; i++){
        phone_a[i] = m[i-1];
        if(i == 8)
          phone_a[8] = n;
      }
    
      for(i = 9; i<=12; i++){
        phone_a[i] = m[i-2];
      }
    
      for(i = 0; i <= 16; i++){
        m[i] = phone_a[i];
      }
    }
    
    int main(){
    
      People student;
    
      scanf("%d %s %s", &student.num, student.name, student.phone);
    
      phone_number(student.phone, '-');
    
      printf("%d\n", student.num);
      printf("%s\n", student.name);
      printf("%s\n", student.phone);
    
      return 0;
    }
    

    void phone_number(char m[], int n) 함수를 이용해서 하이픈(-)을 추가하라길래 위와 같은 방식으로 코딩해봤지만, 런타임에러가 뜨더라구요.

    이 코드를 더 짧게 만들려면 어떻게 해봐야할까요?

    입력 예시

    20211234 홍길동 01012345678
    

    출력 예시

    20211234 
    홍길동 
    010-1234-5678
    
  • 프로필 IBNhyeoli님의 편집
    날짜2021.12.01

    C언어 전화번호 하이픈 추가하는 코딩 조언 부탁드립니다


    #include <stdio.h>
    
    typedef struct people{
      char name[100];
      int num;
      char phone[16];
    }People;
    
    void phone_number(char m[], int n){
      char phone_a[16];
      int i;
      for(i = 0; i <= 3; i++){
        phone_a[i] = m[i];
        if(i == 3)
          phone_a[3] = n;
      }
    
      for(i = 4; i <= 8; i++){
        phone_a[i] = m[i-1];
        if(i == 8)
          phone_a[8] = n;
      }
    
      for(i = 9; i<=12; i++){
        phone_a[i] = m[i-2];
      }
    
      for(i = 0; i <= 16; i++){
        m[i] = phone_a[i];
      }
    }
    
    int main(){
    
      People student;
    
      scanf("%d %s %s", &student.num, student.name, student.phone);
    
      phone_number(student.phone, '-');
    
      printf("%d\n", student.num);
      printf("%s\n", student.name);
      printf("%s\n", student.phone);
    
      return 0;
    }
    

    void phone_number(char m[], int n) 함수를 이용해서 하이픈(-)을 추가하라길래 위와 같은 방식으로 코딩해봤지만, 런타임에러가 뜨더라구요....

    이 코드를 더 짧게 만들려면 어떻게 해봐야할까요?

    입력 예시

    20211234 홍길동 01012345678

    출력 예시

    20211234

    홍길동

    010-1234-5678