2013-10-13 13 views
1

我得到了一些幫助,之前修復了我在這個程序中使用的函數之一,但是現在我處於邏輯的喪失之中。如何實現我的字謎和迴文功能來檢查用戶輸入的字詞?

我在這個程序中有三個目的和兩個功能。第一個目的是打印一個用戶向後輸入的句子。第二個目的是檢查句子中的任何單詞是否與另一個單詞相關。第三個目的是檢查是否有任何一個詞是迴文。

我成功完成了第一個目的。我可以向後打印句子。但現在我不確定我應該如何執行我的功能來檢查任何單詞是否是字謎或迴文。

這是代碼;

/* 
* Ch8pp14.c 
* 
* Created on: Oct 12, 2013 
*  Author: RivalDog 
*  Purpose: Reverse a sentence, check for anagrams and palindromes 
*/ 

#include <stdio.h> 
#include <ctype.h> //Included ctype for tolower/toupper functions 
#define bool int 
#define true 1 
#define false 0 

//Write boolean function that will check if a word is an anagram 
bool check_anagram(char a[], char b[]) 
{ 
    int first[26] = {0}, second[26] = {0}, c = 0; 
// Convert arrays into all lower case letters 
    while(a[c]) 
    { 
     a[c] = (tolower(a[c])); 
     c++; 
    } 

    c = 0; 

    while(b[c]) 
     { 
     b[c] = (tolower(b[c])); 
     c++; 
     } 

     c = 0; 

    while (a[c] != 0) 
    { 
     first[a[c]-'a']++; 
     c++; 
    } 

    c = 0; 

    while (b[c] != 0) 
    { 
     second[b[c]-'a']++; 
     c++; 
    } 

    for (c = 0; c < 26; c++) 
    { 
     if (first[c] != second[c]) 
     return false; 
    } 

    return true; 
} 

//Write boolean function that will check if a word is a palindrome 
bool palindrome(char a[]) 
{ 
    int c=0, j, k; 
    //Convert array into all lower case letters 
    while (a[c]) 
    { 
     a[c] = (tolower(a[c])); 
     c++; 
    } 

    c = 0; 
    j = 0; 
    k = strlen(a) - 1; 
    while (j < k) 
    { 
     if(a[j++] != a[k--]) 
      return false; 
    } 

    return true; 
} 

int main(void) 
{ 
    int i = 0, j = 0, k = 0; 
    char a[80], terminator; 
    //Prompt user to enter sentence, store it into an array 
    printf("Enter a sentence: "); 
    j = getchar(); 
    while (i < 80) 
    { 
     a[i] = j; 
     ++i; 
     j = getchar(); 
     if (j == '!' || j == '.' || j == '?') 
     { 
      terminator = j; 
      break; 
     } 
     else if(j == '\n') 
     { 
      break; 
     } 
    } 
    while(a[k]) 
    { 
     a[k] = (tolower(a[k])); 
     k++; 
    } 
    k = 0; 
    while(k < i) 
    { 
     printf("%c", a[k]); 
     k++; 
    } 
    printf("%c\n", terminator); 
    //Search backwards through the loop for the start of the last word 
    //print the word, and then repeat that process for the rest of the words 
    for(j = i; j >= 0; j--) 
    { 
     while(j > -1) 
     { 
      if (j == 0) 
      { 
       for(k=j;k<i;k++) 
        { 
         printf("%c", a[k]); 
        } 
       printf("%c", terminator); 
        break; 
      } 
      else if (a[j] != ' ') 
       --j; 
      else if (a[j] == ' ') 
       { 
        for(k=j+1;k<i;k++) 
         { 
          printf("%c", a[k]); 
         } 
        printf(" "); 
         break; 
       } 
     } 
     i = j; 
    } 
    //Check if the words are anagrams using previously written function 
    for(i = 0; i < 80; i++) 
    { 
     if (a[i] == ' ') 
     { 

     } 
    } 

    //Check if the words are palindromes using previously written function 

return 0; 
} 

我在想,也許我可以再通過陣列的話通過檢查元素是一個空間搜索,如果是,從搜索的開始空間的指數-1,其中店新數組,重複整個句子的過程,然後在所有數組上調用我的函數。我看到的問題是,我無法真正預測用戶在一個句子中輸入了多少單詞......那麼,如何設置我的代碼以檢查字符串/迴文呢?

謝謝大家!

〜RivalDog

+0

可能是更好的適合[代碼審查網站](http://codereview.stackexchange.com/questions)。 –

+0

您的代碼很難閱讀。你應該使用更高級的函數,如scanf()或fgets(),特別是strtok()。如果使用strtok(),則向後寫入句子非常簡單。我不會責怪你,但是如果你不瞭解strtok(),現在你就知道了。我強烈建議你使用它。有了這段代碼,我懷疑有人會花時間理解它並幫助你。 –

+0

閱讀此[回覆](http://stackoverflow.com/a/17130789/2455888)。你會得到一些想法如何計算字符串中的字數。 – haccks

回答

0

會更好,如果你先優化代碼,使其可讀加入comments.Then您可以在更小的部分劃分問題像 1.How算一個字符串的話呢? 2.如何檢查兩個單詞是否是anagrams? 3.如何檢查一個單詞是否是迴文單詞? 你可以通過谷歌搜索輕鬆獲得這些較小的程序。那麼你的工作就是整合這些答案。希望這可以幫助。

+0

我已經想出瞭如何檢查anagrams和palindromes。代碼中有評論說明了這一點。所以根據你的評論,我要做的就是計算一個字符串中的單詞數量,但這是不正確的。除此之外還有更多的事情要做。一旦我計算出字符串中的單詞數量後,如何通過我的anagram函數發送所有可能的單詞對,並且如何通過我的迴文函數發送每個單詞? – RivalDog

+0

然後,你可以使用某種散列法來處理單詞,其中每個單詞計算一些獨特的散列值並將這些值存儲在某個數組中。然後,如果你遇到一個單詞,那麼你需要計算單詞的哈希值並在數組中搜索anagram目的。例如。 'abcd'散列可以計算爲2 * 3 * 5 * 7,前四個素數的乘法。我的意思是你可以爲每個字符分配一個素數。你需要找出前26個素數。現在,'abcd'的任何組合總會給出3 * 5 * 7 * 11的產品。或者你可以自己想一些更好的散列函數。 – Sarwan

0

要檢查anagram,不需要計算單詞的數量並逐一比較它們或您正在考慮的任何內容。
看看這段代碼。在此代碼功能read_word()正在使用26個元素的int數組讀取字/詞輸入,以跟蹤每個字母被看到多少次,而不是存儲字母本身。另一個函數equal_array()用於檢查數組ab(在main中)是否相等(anagram),並返回布爾值作爲結果。

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

void read_word(int counts[26]); 
bool equal_array(int counts1[26],int counts2[26]); 

int main() 
{ 
    int a[26] = {0}, b[26] = {0}; 

    printf("Enter first word/phrase: "); 
    read_word(a); 
    printf("Enter second word/phrase: "); 
    read_word(b); 

    bool flag = equal_array(a,b); 
    printf("The words/phrase are "); 
    if(flag) 
     printf("anagrams"); 
    else 
     printf("not anagrams"); 

    return 0; 
} 

void read_word(int counts[26]) 
{ 
    int ch; 
    while((ch = getchar()) != '\n') 
    if(ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') 
     counts[toupper(ch) - 'A']++; 
} 

bool equal_array(int counts1[26],int counts2[26]) 
{ 
    int i = 0; 
    while(i < 26) 
    { 
     if(counts1[i] == counts2[i]) 
      i++; 
     else 
      break; 
    } 

    return i == 26 ? true : false; 
} 
相關問題