从c中的字符串中删除标点符号和空格?

从c中的字符串中删除标点符号和空格?

问题描述:

这是在学校作业。我不是在向正确的方向推动正确的答案。关于为什么会出现这种错误的解释会很好,并解释正确的方法。这个C程序应该做的是在用户输入中读取没有空格和标点符号并将其分配给字符数组字符串。这个数组然后应该被传入函数回文。 Palindrome应该是字符串的长度,如果等于1或0,则返回TRUE或1,然后检查字符串的第一个和最后一个字符。如果它们匹配,则检索第二个和第二个到最后一个字符以及中间的所有字符,并将其传递到函数回文。从c中的字符串中删除标点符号和空格?

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

#define TRUE 1 
#define FALSE 0 
typedef int Bool; 

Bool palindrome(char str[]); 
main() 
{ 
char string[1000], ch; 
int i = 0; 

printf("Enter a message: "); 
while((ch = getchar()) != '\n'){ 
    if(isspace(ch)==FALSE || ispunct(ch)==FALSE); 
    string[i] = tolower(ch); 
    i++; 
} 

string[i] = '\0'; 

printf("\n"); 
if(palindrome(string)) 
    printf("Palindrome\n"); 
else 
    printf("Not a palindrome\n); 

return 0; 
} 

Bool palindrome(char str[]) 
{ 
    int length = strlen(str); 
    if((length == 1) || (length == 0)) 
    return TRUE; 
    else 
    { 
    if((str[0] == str[length - 1]) 
     { 
     char str_new[length-1]; 
     int i, j; 
     for(i = 1, j = 0; str[i]!=str[length-1]; i++, j++) 
      str_new[j] = str[i]; 

     str_new[i] = '\0'; 
     palindrome(str_new); 
     } 
     else 
     return FALSE; 

    } 
} 

不管它输入的东西总是打印出来的字符串是非回文。例如,当我输入

他作为一个魔鬼生活,呃?

它打印出

不是回文

此外,当我编辑的程序使用先前的输入是

检查什么是数组串他是一个魔鬼,呃?

随意评论我的代码的任何其他方面,你看到代码使用改进。他们确实没有提供任何其他的东西,除了是它的权利或不是我们的代码。

编辑:
我做了检查,看看char数组中的值是什么。我在最后一次报名前就说过了。

+0

提示:在将消毒过的字符串传递给'回文()',打印出来。然后你会知道你是否正确清理了它。 – alexis 2013-03-23 16:56:28

+0

'if(!isspace(ch)&&!ispunct(ch))'或更好:'if(isletter(ch))' – alexis 2013-03-23 17:01:21

+0

@user我想你的问题已经被回答了:http://meta.stackexchange.com/questions/5234/ – 2013-03-23 18:05:02

这里看看......

while((ch = getchar()) != '\n'){ 
    if(isspace(ch)==FALSE || ispunct(ch)==FALSE); 
    string[i] = tolower(ch); 
    i++; 
} 

注意 ';'在if语句结尾处。那'''导致字符串[i] = tolower(ch)总是执行。另外,你的逻辑是不正确的,如果这个字符不是空格,并且它不是标点符号,你希望代码执行。

另外,请注意您的缩进。 i ++也应该放在if语句中,但是它缺少大括号。因此,即使您删除了';',i ++仍然会始终执行。所以......

while((ch = getchar()) != '\n'){ 
    if(isspace(ch)==FALSE && ispunct(ch)==FALSE) 
    { 
    string[i] = tolower(ch); 
    i++; 
    } 
} 

或者......甚至更好的还是...

while((ch = getchar()) != '\n'){ 
    if(isspace(ch)==FALSE && ispunct(ch)==FALSE) 
    string[i++] = tolower(ch); 
} 

一个风格的注释,以及...它往往是一个好主意,有从功能单一的退出点为了维护和可读性。其他人可能在那里有不同的争论,但这是我过去30年来在DoD工作中生活的那条硬性规定。看看这个可读性,看看它是否对你更有意义。

Bool palindrome(char str[]) 
{ 
    Bool result = TRUE; 
    int length = strlen(str); 

    if(length > 1 && str[0] == str[length-1]) 
    { 
    char str_new[length-1]; 
    int i, j; 

    for(i = 1, j = 0; str[i]!=str[length-1]; i++, j++) 
     str_new[j] = str[i]; 

    str_new[i] = '\0'; 
    result = palindrome(str_new); 
    } 

    return result; 
} 

最后......但从效率来看,而不是复制字符串,你可以很容易地索引它...

Bool palindrome(char str[]) 
{ 
    Bool result = TRUE; 
    int length = strlen(str); 

    if(length > 1 && str[0] == str[length-1]) 
    { 
     str[length-1] = '\0'; 
     result = palindrome(&str[1]); 
    } 

    return result; 
} 
+0

if中的逻辑错误,回文函数太复杂。 – 2013-03-23 17:12:51

+0

所以现在我们已经告诉过你如何解决这个问题,并且你已经完成了。做得好。但是你的“回文”功能根本不起作用。对于我的生活,我无法理解为什么这个答案得到了提升。 – 2013-03-23 17:21:32

if(isspace(ch)==FALSE || ispunct(ch)==FALSE); 

这是你的错误。首先,最后不应该有分号(;)。

其次,你不应该使用或者,你应该使用因为你要确保你过滤除字母一切:

​​

另外:

for(i = 1, j = 0; str[i]!=str[length-1]; i++, j++) 

在这里,布尔表达式是错误的。你应该评估j

for(i = 1, j = 0; j < length - 1; i++, j++) 

最后,您对i++声明的定位,同时要追加字符到新字符串不正确。将它们放回原处,以便编译器知道它需要成为while循环体的一部分,并且在if块之外。

一些明显的要点:

  • 在这个问题的代码不会编译。
  • 您的主要声明不是标准的。
  • if声明在行尾有一个错误的分号。
  • if语句中的逻辑错误。逻辑或测试将始终评估为真,因为一个字符不能既是空格也是标点符号。
  • 您的回文检查功能比需要的复杂得多。

的关键变化,使的是,你的if的说法应该是像这样:

if (!isspace(ch) && !ispunct(ch)) 

一个完整的工作程序是这样的:

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

#define TRUE 1 
#define FALSE 0 
typedef int Bool; 

Bool palindrome(char str[]); 

int main(void) 
{ 
    char string[1000], ch; 
    int i = 0; 

    printf("Enter a message: "); 
    while((ch = getchar()) != '\n'){ 
     if (!isspace(ch) && !ispunct(ch)) 
     { 
      string[i] = tolower(ch); 
      i++; 
     } 
    } 

    string[i] = '\0'; 
    printf("string = %s\n", string); 
    if(palindrome(string)) 
     printf("Palindrome\n"); 
    else 
     printf("Not a palindrome\n"); 

    return 0; 
} 

Bool palindrome(char str[]) 
{ 
    int left = 0; 
    int right = strlen(str)-1; 
    while (left<right) 
    { 
     if(str[left] != str[right]) 
      return FALSE; 
     left++; 
     right--; 
    } 
    return TRUE; 
} 

这里是输出:

 
Enter a message: He lived as a devil, eh? 
string = helivedasadevileh 
Palindrome 
+0

如果你不关心命令行参数,主要语句是完全合法的 – 2013-03-23 17:15:00

+0

@KScottPiel我向你推荐这个问题:http://*.com/questions/2108192/what-are-the-valid-signatures-for- CS-主功能 – 2013-03-23 17:17:28