ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 과제 : 문자열치환
    잡다 2014. 4. 8. 14:09

     

     

    처음 한 소스

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

    char * Change_word(char * string, char * old_word, char *new_word);

    int main()
    {
      char  caOld_word[32];
      char  caNew_word[32];
      char  caString_word[50];

      fputs("Input String : ", stdout);
      fgets(caString_word, 31, stdin);
      fflush(stdin);

      fputs("Choose String to modify : ", stdout);
      fgets(caOld_word, 11, stdin);
      fflush(stdin);

      fputs("Input String to modify : ", stdout);
      fgets(caNew_word, 11, stdin);
      fflush(stdin);

      Change_word(caString_word, caOld_word, caNew_word);

      printf("Output String : %s \n", caString_word);

      getchar();

      return 0;
    }

    char * Change_word(char * string, char * old_word, char *new_word)
    {
      int  iLength_old = 0;
      int  iLength_new = 0;
      int  iCnt_old = 0;
      int  iCnt_new = 0;
      int  iCnt1 = 0;
      int  iCnt2;
      int  iCnt_temp;
      char  caTemp[50= "";

      // ----------------------------- // 수정 문자 길이 시작
      while('\n' != old_word[iLength_old] && 0 != old_word[iLength_old] )
      {
        ++iLength_old;
      }

      while('\n' != new_word[iLength_new] && 0 != new_word[iLength_new] )
      {
        ++iLength_new;
      }

      // ---------------------------- // 수정 문자 길이 종료

      // ---------------------------- // 문자열 찾아 바꾸기 시작
      while(0 != string[iCnt1])
      {
        if(string[iCnt1] == old_word[iCnt_old]) //--- 첫 문자가 맞으면 뒷문자 확인
        {
          ++iCnt_old;
          for(iCnt2 = 1; iLength_old > iCnt2; ++iCnt2)
          {
            
            if(string[iCnt2+iCnt1] == old_word[iCnt_old])
            {
              if(iCnt_old == iLength_old-1)  //--- 끝까지 다 일치하면 치환
              {
                strcpy(caTemp, string);

                for(iCnt2 = 0; iLength_new > iCnt2; ++iCnt2)  //--- new로 바꾸기
                {
                  string[iCnt2+iCnt1] = new_word[iCnt_new];
                  ++iCnt_new;
                }

                iCnt_temp = iCnt1 + iLength_old;
                iCnt2 = iCnt2 + iCnt1;

                while(0 != caTemp[iCnt_temp])   //--- 나머지 문자열 넣기
                {
                  string[iCnt2] = caTemp[iCnt_temp];
                  ++iCnt2;
                  ++iCnt_temp;
                }
                string[iCnt2] = 0;

                return string;  //--- 함수 끝
              }
              else
              {
                ++iCnt_old;
              }
            }
            else
            {
              break;
            }
          }
          iCnt_old = 0;
        }
        ++iCnt1;
      }
      // ---------------------------- // 문자열 찾아 바꾸기 종료

      return string;
    }

    main_before.exe

     

     

    문자열 함수를 사용한 코드

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

    char * Change_word(char * string, char * old_word, char *new_word);

    int main()
    {
      char  caOld_word[32];
      char  caNew_word[32];
      char  caString_word[50];

      fputs("Input String : ", stdout);
      fgets(caString_word, 31, stdin);
      fflush(stdin);

      fputs("Choose String to modify : ", stdout);
      fgets(caOld_word, 11, stdin);
      fflush(stdin);

      fputs("Input String to modify : ", stdout);
      fgets(caNew_word, 11, stdin);
      fflush(stdin);

      Change_word(caString_word, caOld_word, caNew_word);

      printf("Output String : %s \n", caString_word);

      getchar();

      return 0;
    }

    char * Change_word(char * string, char * old_word, char *new_word)
    {
      int  iCnt;
      int  iCnt2;
      int  iCntOld = 0;
      int  iLengthNew = strlen(new_word);
      int  iLengthOld = strlen(old_word);
      char  temp[50];

      if('\n' == new_word[iLengthNew-1])
      {
        --iLengthNew;
      }

      if('\n' == old_word[iLengthOld-1])
      {
        --iLengthOld;
      }

      for(iCnt = 00 != string[iCnt]; ++iCnt)
      {
        if(string[iCnt] == old_word[iCntOld])  // 첫번째 문자 일치?
        {
          iCnt2 = 1;  // 두번째 부터 시작
          iCntOld = 1;  //       ``

          while(iCnt2 < iLengthOld)  // 나머지 문자 일치
          {
            if(string[iCnt2+iCnt] == old_word[iCntOld])
            {
              if(iCnt2 == iLengthOld-1)  // 마지막 문자인가?
              {
                strcpy(temp, &string[iCnt+iCnt2+1]);
                strcpy(&string[iCnt], new_word);
                strcpy(&string[iCnt+iLengthNew], temp);

                return string;
              }
              ++iCnt2;
              ++iCntOld;
            }
            else
            {
              iCntOld = 0;
              break;
            }
          }
        }
      }

      return string;
    }

    main.exe

     

    '잡다' 카테고리의 다른 글

    간단한 포토샵  (0) 2014.07.21
    Raw socket  (0) 2014.06.28
    Mac Address, 제조회사  (0) 2014.06.27
    [vi명령어] 자동정렬 / 단어찾기 / 도스저장  (0) 2014.06.25
    GNU Compiler __attribute__  (0) 2014.06.19
Designed by Tistory.