[英]How can I implement my anagram and palindrome functions to check the words input by a user?
在更早地修復此程序中使用的功能之一時,我得到了一些幫助,但現在我迷失了邏輯。
在此程序中,我有三個目的和兩個功能。 第一個目的是打印用戶向后輸入的句子。 第二個目的是檢查句子中是否有任何單詞與另一個字母相讀。 第三個目的是檢查是否有一個單詞是回文。
我成功地完成了第一個目標。 我可以向后打印句子。 但是現在我不確定如何執行我的功能來檢查任何單詞是否是字謎或回文。
這是代碼;
/*
* 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;
}
我在想,也許我可以通過檢查元素是否為空格來再次在數組中搜索單詞,如果是,則將搜索開始的位置存儲到新數組中空間的index-1處,對整個句子,然后在所有數組上調用我的函數。 我看到的問題是我無法真正預測用戶將在一個句子中輸入多少個單詞...那么,如何設置我的代碼以檢查字謎/回文?
謝謝大家!
〜RivalDog
如果先優化代碼並通過添加注釋使其可讀性會更好。然后可以將問題分成較小的部分,例如1.如何計算字符串中的單詞? 2.如何檢查兩個詞是否是字謎? 3.如何檢查單詞是否是回文? 而這些較小的程序可以通過Googling輕松獲得。 那么您的工作將只是整合這些答案。 希望這可以幫助。
要檢查字謎,無需計算單詞數並將其一一比較或您在考慮什么。
看這段代碼。 在此代碼函數中, read_word()
使用包含26個元素的int
數組讀取單詞/短語輸入,以跟蹤每個字母被查看了多少次,而不是存儲字母本身。 另一個函數equal_array()
用來檢查數組a
和b
(在main
)是否相等(字謎),並返回一個布爾值作為結果。
#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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.