齐小白 2014-08-14 02:58
浏览 740

有关回文代码的理解问题?

以下是判断一个字符串是否为一个回文,请问这段代码是如何忽略标点符号的?

#include
#include
#include

#define YES 1
#define NO 0

/* stackNode structure definition /
struct stackNode {
char data; /
node data /
struct stackNode *nextPtr; /
pointer to next node /
}; /
end struct stackNode */

typedef struct stackNode STACKNODE;
typedef STACKNODE *STACKNODEPTR;

/* function prototypes */
void push( STACKNODEPTR *topPtr, char info );

char pop( STACKNODEPTR *topPtr );
int isEmpty( STACKNODEPTR topPtr );

int main( void )
{
STACKNODEPTR stackPtr = NULL; /* points to the stack top /
char c; /
current character from text /
char line[ 50 ]; /
text from user /
char condensedLine[ 50 ]; /
text with only letters /
int i = 0; /
length of condensed line /
int j = 0; /
length of line /
int palindrome = YES; /
result of palindrome test */

printf( "Enter a line of text:\n" );

/* read each letter with getchar and add to line */
while ( ( c = getchar() ) != '\n' ) {
line[ j++ ] = c;

  /* remove all spaces and punctuation */
  if ( isalpha( c ) ) { 
     condensedLine[ i++ ] = tolower( c );
     push( &stackPtr, tolower( c ) );
  } /* end if */

} /* end while */

line[ j ] = '\0';

/* loop through condensedLine */
for ( j = 0; j < i; j++ ) {

  /* if condensedLine does not equal stack */
  if ( condensedLine[ j ] != pop( &stackPtr ) ) { 
     palindrome = NO;
     break; /* exit loop */
  } /* end if */

} /* end for */

/* if text is a palindrome /
if ( palindrome ) {
printf( "\"%s\" is a palindrome\n", line );
} /
end if /
else {
printf( "\"%s\" is not a palindrome\n", line );
} /
end else */

return 0; /* indicate successful termination */

} /* end main */

/* Insert a node at the stack top /
void push( STACKNODEPTR *topPtr, char info )
{
STACKNODEPTR newPtr; /
temporary node pointer */

/* dynamically allocate memory */
newPtr = malloc( sizeof( STACKNODE ) );

/* if memory was allocated, insert node at top of stack /
if ( newPtr ) {
newPtr->data = info;
newPtr->nextPtr = *topPtr;
*topPtr = newPtr;
} /
end if /
else {
printf( "%d not inserted. No memory available.\n", info );
} /
end else */

} /* end function push */

/* Remove a node from the stack top /
char pop( STACKNODEPTR *topPtr )
{
STACKNODEPTR tempPtr; /
temporary node pointer /
int popValue; /
value of popped node */

tempPtr = topPtr;
popValue = ( *topPtr )->data;
*topPtr = ( *topPtr )->nextPtr; /
reset topPtr /
free( tempPtr ); /
free memory */

return popValue; /* return value of popped node */

} /* end function pop */

/* Is the stack empty? /
int isEmpty( STACKNODEPTR topPtr )
{
return !topPtr; /
return NULL if stack is empty */

} /* end function isEmpty */

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 Python时间序列如何拟合疏系数模型
    • ¥15 求学软件的前人们指明方向🥺
    • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
    • ¥20 双层网络上信息-疾病传播
    • ¥50 paddlepaddle pinn
    • ¥20 idea运行测试代码报错问题
    • ¥15 网络监控:网络故障告警通知
    • ¥15 django项目运行报编码错误
    • ¥15 STM32驱动继电器
    • ¥15 Windows server update services