2016-10-30 2 views
-1

문자열에서 단어를 변경하는 방법을 찾고 있습니다. 첫 단어보다 더 많은 기호를 가진 다른 단어로 단어를 변경하고 싶습니다. 먼저 다른 단어로 바꾸고 싶은 단어의 기호를 세웁니다. 이 단어가 내 프로그램에있는 텍스트에 얼마나 많은 문자 배열로 있는지 (문자 배열) 이제이 단어를 변경하고 싶습니다. 더 많은 상징이있는 단어. 이 코드입니다 : 나는 단어가 있었다 입력하면C에서 다른 단어로 문자열의 단어 변경

#include "stdafx.h" 
#include <iostream> 
using namespace std; 

int main() 
{ 
    Start: 
    printf("This program can change word or symbols combanation in text\n\n"); 
    char text[550] = "There was a grocery shop in a town. Plenty of mice lived in that grocery shop.\nFood was in plenty for them. hey also wasted the bread\nbiscuits and fruits of the shop.\nThe grocer got really worried. So, he thought I should buy a cat\nand let it stay at the grocery. Only then I can save my things.\nHe bought a nice, big fat cat and let him stay there. The cat had a nice\nime hunting the mice and killing them.\nThe mice could not move freely now. They were afraid that\n\n"; 
    printf("%s\n", text); 
    int symbols_in_text = 0; 
    for (int f = 0;f < 550;f++) 
    { 
     if (text[f]) 
     { 
      symbols_in_text++; 
     } 
     else 
     { 
      break; 
     } 
    } 
    printf("Text has %i symbols\n\n", symbols_in_text); 
    char b[15]; 
    printf("Which word would you like to chenge in text above?\n(Maximum quantity of Symbols is 15)\nType word or some symbol(s)\n--->>"); 
    cin >> b; 
    int word_symbols=0; 
    for (int a = 0;a < 15;a++) 
    { 
     if (b[a]) 
     { 
      word_symbols++; 
     } 
     else 
     { 
      break; 
     } 
    } 
    printf("Word which you have entered has %i symbols\n", word_symbols); 
    int word_in_text = 0; 
    for (int i = 0; i < 550 - word_symbols; i++) 
    { 
     int found = 1; 
     for (int j = 0; j < word_symbols; j++) 
     { 
      if (b[j] != text[i + j]) 
      { 
       found = 0; 
       break; 
      } 
     } 
     if (found && text[i + word_symbols]==' ') 
     { 
      word_in_text++; 
     } 
    } 
    printf("Founded %i %s as the word an is Not the some part of the other word\n", word_in_text,b); 
    if (word_in_text != 0) 
    { 
     char input_word[15]; 
     printf("Enter the word which you want to insert for first entered word\nWarning:You have to enter the world which have %i symbols\n--->>", word_symbols); 
     cin >> input_word; 
     int in_word_symbols = 0; 
     for (int a = 0;a < 15;a++) 
     { 
      if (input_word[a]) 
      { 
       in_word_symbols++; 
      } 
      else 
      { 
       break; 
      } 
     } 
     printf("The word which you have entered has %i symbols\n\n", in_word_symbols); 
     if (word_symbols == in_word_symbols) 
     { 
      for (int i = 0; i < 550 - word_symbols; i++) 
      { 
       int found = 1; 
       for (int j = 0; j < word_symbols; j++) 
       { 
        if (b[j] != text[i + j]) 
        { 
         found = 0; 
         break; 
        } 
       } 
       if (found != 0 && text[i + word_symbols] == ' ') 
       { 
        for (int j = 0; j < in_word_symbols; j++) 
        { 
         text[i + j] = input_word[j]; 
        } 

       } 
      } 
      printf("The result is--->>\n%s\n\n//////////////////////////////////////////END OF////////////////////////////////////////\n\n", text); 
     } 
     else if (in_word_symbols > word_symbols) 
     { 
      int text_char_index = 0; 
      step1: 
      for (int count = 0; count < 550 - word_symbols; count++) 
      { 
       text_char_index++; 
       int found = 1; 
       for (int j = 0; j < word_symbols; j++) 
       { 

        if (b[j] != text[count + j]) 
        { 
         found = 0; 
         break; 
        } 

        if (found != 0 && text[count + word_symbols] == ' ') 
        { 
         count = text_char_index-1; 
         goto index_changing; 
         insert: 
          for (int c = 0; c < in_word_symbols; c++) 
          { 
           text[count + c] = input_word[c]; 
          } 
          if (count > 500) 
          { 
           goto printing_result; 
          } 
          else 
          { 
           continue; 
          } 
        } 
       } 
      } 
     index_changing: 

       for (int l = 466; l > text_char_index + word_symbols; --l) 
       { 
        text[l] = text[l + 1]; 
       } 
       goto insert; 
      printing_result: 
      printf("The result is--->>\n%s\n\n//////////////////////////////////////////END OF////////////////////////////////////////\n\n", text); 
     } 
    } 
    goto Start; 

    return 0; 
} 

(- 그것은 내가 텍스트로 변경하고자하는 단어이다) 내가 첫 번째 단어를 입력보다 더 많은 문자가 감지 단어를 (입력보다 - 그것은 내가 답례로 텍스트를 삽입 할 단어입니다) "이었다"

콘솔 출력은 다음과 같습니다있다

검출 그리고 그런이 코드 방법 또는 뭔가 사용할 수 없습니다 , if() 및 for 루프 만 사용할 수 있습니다. 누구나 어떻게 도와 줄 수 있습니까?

+0

C 또는 C++? 코드가 두 언어의 끔찍한 혼합으로 보입니까? –

+0

나는 C 언어를 배우고 있지만 scanf()를 모르므로 C++ lib 및 namespace t cin >>을 추가해야합니다. – AlexL

+3

SO는 디버깅 서비스가 아닙니다. 심볼로 컴파일하고 디버거 내부에서 코드를 실행하여 프로그램을 한 줄씩 추적하여 관련 변수의 값을 검사하여 실제 진행 상황을 파악합니다. 만약 * 특정 * 질문이 생기면 여기로 다시 오십시오. – alk

답변

0

먼저이 규칙에서 문자열 함수/메서드를 사용할 수 없으므로 주로 if 문과 루프를 사용하여 코드를 명시 적으로 작성해야합니다.

원래 단어보다 큰 대체 단어를 처리하기 위해 별도의 전체 코드 블록을 작성하는 대신 원래 대체 코드의 일부로 처리하십시오. 대체 단어가있는 가능성도 처리하십시오. 더 작다. 둘 모두 원 단어와 대체 단어의 차이에 따라 원본 텍스트를 한 방향으로 또는 다른 방향으로 옮겨야합니다.

위 코드를 다시 작성하여 다양한 스타일을 변경하고 버그를 수정했습니다. 또한 그것을 순수 'C'프로그램을 만드는 :

#include <stdio.h> 
#include <stdbool.h> 

int main() 
{ 
    printf("This program can change word or character combinations in text.\n\n"); 

    char text[1024] = 
     "There was a grocery shop in a town. Plenty of mice lived in that grocery shop.\n" 
     "Food was in plenty for them. They also wasted the bread, biscuits and fruits\n" 
     "of the shop. The grocer got really worried. So, he thought I should buy a cat\n" 
     "and let it stay at the grocery. Only then I can save my things. He bought a\n" 
     "nice, big fat cat and let him stay there. The cat had a nice time hunting mice\n" 
     "and killing them. The mice could not move freely now. They were afraid that\n"; 

    while (true) { 
     printf("%s\n", text); 

     int characters_in_text = 0; 

     for (int f = 0; text[f] != '\0'; f++) // not allowed to use strlen() 
     { 
      characters_in_text++; 
     } 

     printf("Text has %i characters.\n\n", characters_in_text); 

     char word[17]; 
     printf("Which word would you like to change in text above?\n"); 
     printf("(Maximum quantity of characters is %d)\n", (int) sizeof(word) - 2); 
     printf("Type word or some character(s)\n"); 
     printf("--->> "); 

     (void) fgets(word, sizeof(word), stdin); 

     int a, word_characters = 0; 

     for (a = 0; word[a] != '\n'; a++) 
     { 
      word_characters++; 
     } 

     word[a] = '\0'; 

     if (word_characters == 0) 
     { 
      break; // exit program 
     } 

     printf("Word which you have entered has %i characters.\n", word_characters); 

     int words_in_text = 0; 

     for (int i = 0; i < characters_in_text - word_characters; i++) 
     { 
      bool found = true; 

      for (int j = 0; j < word_characters; j++) 
      { 
       if (word[j] != text[i + j]) 
       { 
        found = false; 
        break; 
       } 
      } 

      if (found) 
      { 
       char next_letter = text[i + word_characters]; 

       if (next_letter == ' ' || next_letter == '.' || next_letter == ',' || next_letter == '\n' || next_letter == '\0') 
       { 
        words_in_text++; 
       } 
      } 
     } 

     printf("Found %i instance(s) of %s that are not part of another word.\n", words_in_text, word); 

     char replacement_word[17]; 
     printf("Enter the word which you want to insert for first entered word.\n"); 
     printf("(Maximum quantity of characters is %d)\n", (int) sizeof(replacement_word) - 2); 
     printf("Type word or some character(s)\n"); 
     printf("--->> "); 

     (void) fgets(replacement_word, sizeof(replacement_word), stdin); 

     int replacement_word_characters = 0; 

     for (a = 0; replacement_word[a] != '\n'; a++) 
     { 
      replacement_word_characters++; 
     } 

     replacement_word[a] = '\0'; 

     printf("The word which you have entered has %i characters.\n\n", replacement_word_characters); 

     int text_shift = replacement_word_characters - word_characters; 

     for (int i = 0; i < characters_in_text - word_characters; i++) 
     { 
      bool found = true; 

      for (int j = 0; j < word_characters; j++) 
      { 
       if (word[j] != text[i + j]) 
       { 
        found = false; 
        break; 
       } 
      } 

      if (found) 
      { 
       char next_letter = text[i + word_characters]; 

       if (next_letter == ' ' || next_letter == '.' || next_letter == ',' || next_letter == '\n' || next_letter == '\0') 
       { 
        if (text_shift > 0) 
        { 
         for (int k = characters_in_text; k > i + word_characters - 1; k--) 
         { 
          text[k + text_shift] = text[k]; 
         } 

        } 
        else if (text_shift < 0) 
        { 
         for (int k = i + word_characters; k < characters_in_text + 1; k++) 
         { 
          text[k + text_shift] = text[k]; 
         } 

        } 

        characters_in_text += text_shift; 

        for (int j = 0; j < replacement_word_characters; j++) 
        { 
         text[i + j] = replacement_word[j]; 
        } 
       } 
      } 
     } 
    } 

    return 0; 
} 
당신은이 코드는 라이브러리 함수없이 얻는 방법을 복잡하게 볼 수 있습니다

-ispunct() 같은 함수에서와 훨씬 쉬울 것 단어를 다음 가능한 문장을 테스트 ctype.h

또한 원래 코드에서 많은 유용한 값을 계산했지만 나중에 코드에서 적절히 사용하지 못했습니다.

그러나해야 할 일 :이 코드에 추가 할 오류 검사가 많이 있습니다. 대체 텍스트로 인해 텍스트가 포함 된 배열보다 텍스트가 커지면 어떻게해야합니까? 단어 뒤에 문자를 테스트하여 단어가 다른 단어에 포함되어 있지 않은지 확인하지만 작업 전에 문자를 테스트하지 못합니다. '다른'대 '다른'.

+0

내 질문에 대한 관심과 노력을 많이 주셔서 감사합니다. – AlexL