[英]Find palindromes in sentence
我正在尝试编写一段 C 代码,该代码接受一个句子并返回该句子中的所有回文,每个回文都在一个新行中。 例如,句子“I like to Race a civic Racecar”将返回:civic Racecar
我尝试使用一些调试软件(lldb,因为我是 mac 用户),但发现它有点混乱。 下面的代码是我写的。 它返回一个分段错误,我无法在我的程序中识别它。
int is_palin(char c[], int length)
{
int front = 0;
int back = length - 1; /* account for length starting at 0 */
if (length % 2 == 0){ /* check for even palindromes */
int middle = (length /2) -1 ;
while (front< middle + 1){
if (c[front] != c[back]){
return 0;}
front = front + 1;
back = back -1;
}
}
else { /* check for odd palindromes */
int middle = ((back - 2) / 2 ) + 1;
while (front != middle){
if (c[front] != c[back]){
return 0;}
front = front + 1;
back = back -1;}
}
return 1;
}
int is_delimiting_char(char ch)
{
if(ch == ' ') //White space
return 1;
else if(ch == ',') //Comma
return 1;
else if(ch == '.') //Period
return 1;
else if(ch == '!') //Exclamation
return 1;
else if(ch == '?') //Question mark
return 1;
else if(ch == '_') //Underscore
return 1;
else if(ch == '-') //Hyphen
return 1;
else if(ch == '(') //Opening parentheses
return 1;
else if(ch == ')') //Closing parentheses
return 1;
else if(ch == '\n') //Newline (the input ends with it)
return 1;
else
return 0;
}
/////////////////////////////////////////////
//---------------------------------------------------------------------------
// MAIN function
//---------------------------------------------------------------------------
int main (int argc, char** argv) {
char input_sentence[100];
int i=0;
char current_char;
int delimiting_char;
char word[20];
int word_length;
int have_palindrome = 0;
/////////////////////////////////////////////
/////////////////////////////////////////////
/* Infinite loop
* Asks for input sentence and prints the palindromes in it
* Terminated by user (e.g. CTRL+C)
*/
while(1) {
i=0;
print_char('\n');
print_string("input: ");
/* Read the input sentence.
* It is just a sequence of character terminated by a new line (\n) character.
*/
do {
current_char=read_char();
input_sentence[i]=current_char;
i++;
} while (current_char != '\n');
/////////////////////////////////////////////
print_string("output:\n");
int char_index = 0;
for(int k=0; k<i; k++) {
palin = 1;
current_char = input_sentence[k];
delimiting_char = is_delimiting_char(current_char);
if(delimiting_char) {
if (char_index > 0) { //Avoids printing a blank line in case of consecutive delimiting characters.
word[char_index++] = '\n'; //Puts an newline character so the next word in printed in a new line.
word_length = word_length + 1;
if (is_palin(word, word_length) && word_length > 1){
have_palindrome = 1;
for(int j=0; j<char_index; j++) {
print_char(word[j]);
}
word_length = 0;
char_index = 0;
}
} }
else {
word[char_index++] = current_char;
word_length = word_length + 1;
}
}
if (have_palindrome == 0){
print_string("Sorry! No palindromes found!"); }
}
return 0;
}
还想知道是否有人有很好的视频或网站来学习如何使用 lldb,当人们以前从未使用过这种类型的任何东西时。 谢谢!
这里有几个错误:
word_length
在第一次使用时未初始化,因此像word_length = word_length + 1
这样的语句会导致未定义的行为。 事实上,您有两个不同的变量, char_index
和word_length
,它们应该始终具有相同的值。 与其费心让它们保持同步,不如只使用一个变量。char_index
和word_length
重置为零。 当然,您应该在每个单词之后重置。palin = 1;
可能是旧代码的剩余部分。 您还应该在每行之后重置have_palindrome
。 通常,在定义变量时应该更加小心。read_char
读取的代码(可能是getchar
的别名)需要检查输入的结尾。front < back
的条件并完成它。 奇怪大小的回文的中间字符无关紧要。 (这不是错误,您的代码只是不必要地复杂。)这是我在句子中查找和打印回文的代码:
#include<stdio.h>
#include<string.h>
int pal(char*);
int main()
{
char s[100];
printf("Enter the sentence:-\n");
fgets(s,100,stdin);
char temp[100];
int i,j=0;
printf("The palindromes are:-\n");
for(i=0; s[i]!='\0'; i++){
if(s[i]==' '){
temp[j]='\0';
pal(temp);
j=0;
continue;
}
else{
temp[j]=s[i];
j++;
}
}
printf("\n");
return 0;
}
int pal(char temp[100])
{
int i;
int len=strlen(temp);
for(i=0; i<len/2; i++){
if(temp[i]==temp[len-1-i])
continue;
else
return 1;
}
fputs(temp,stdout);
printf(" ");
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.