用栈判断字符是否中心对称
时间: 2023-12-17 17:03:47 浏览: 37
可以使用栈来判断字符是否中心对称。具体做法是将字符串的前一半依次入栈,然后从字符串的中间位置开始遍历后一半字符,每次取出栈顶元素与当前字符比较,如果相同则继续遍历,否则返回false。
以下是示例代码:
```python
def is_symmetric(s):
n = len(s)
stack = []
for i in range(n // 2):
stack.append(s[i])
j = n // 2 if n % 2 == 0 else n // 2 + 1
while j < n:
if not stack or stack.pop() != s[j]:
return False
j += 1
return True
```
相关问题
c语言使用栈判断字符串是否中心对称
可以使用栈来判断字符串是否中心对称。具体做法是将字符串的前一半压入栈中,然后依次比较栈中的字符和字符串后一半的字符是否相等。如果相等,则继续比较,否则说明不是中心对称的字符串。以下是示例代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_LEN 100
int main() {
char str[MAX_LEN];
int len, i, mid;
char stack[MAX_LEN / 2];
int top = -1;
printf("请输入一个字符串:");
scanf("%s", str);
len = strlen(str);
mid = len / 2;
for (i = 0; i < mid; i++) {
stack[++top] = str[i];
}
if (len % 2 == 1) {
i = mid + 1;
} else {
i = mid;
}
while (i < len) {
if (stack[top] == str[i]) {
top--;
i++;
} else {
break;
}
}
if (top == -1) {
printf("%s 是中心对称的字符串\n", str);
} else {
printf("%s 不是中心对称的字符串\n", str);
}
return 0;
}
```
注意:这段代码只是一个示例,实际应用中需要考虑更多的情况,比如字符串长度为奇数和偶数的情况,以及字符串中可能包含空格等特殊字符的情况。
用顺序栈编写函数判断字符串是否为对称串
好的,我可以回答这个问题。以下是用顺序栈编写判断字符串是否为对称串的函数:
```C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACK_INIT_SIZE 20
#define STACK_INCREMENT 10
typedef struct {
char* base;
char* top;
int stacksize;
} SqStack;
void InitStack(SqStack* S) {
(*S).base = (char*) malloc(STACK_INIT_SIZE * sizeof(char));
if (!(*S).base) {
printf("Memory allocation failed.\n");
exit(1);
}
(*S).top = (*S).base;
(*S).stacksize = STACK_INIT_SIZE;
}
void Push(SqStack* S, char e) {
if ((*S).top - (*S).base >= (*S).stacksize) {
(*S).base = (char*) realloc((*S).base,
((*S).stacksize + STACK_INCREMENT) * sizeof(char));
if (!(*S).base) {
printf("Memory allocation failed.\n");
exit(1);
}
(*S).top = (*S).base + (*S).stacksize;
(*S).stacksize += STACK_INCREMENT;
}
*((*S).top++) = e;
}
char Pop(SqStack* S) {
if ((*S).top == (*S).base)
return '\0';
return *(--(*S).top);
}
int IsEmpty(SqStack* S) {
if ((*S).top == (*S).base)
return 1;
return 0;
}
int SymmetricString(char* s) {
SqStack S;
InitStack(&S);
int i, len = strlen(s);
for (i = 0; i < len / 2; i++) {
Push(&S, s[i]);
}
if (len % 2 == 1)
i++;
while (i < len && Pop(&S) == s[i]) {
i++;
}
if (IsEmpty(&S))
return 1;
else
return 0;
}
int main() {
char s[100];
printf("Please input a string: ");
scanf("%s", s);
if (SymmetricString(s))
printf("The string is symmetric.\n");
else
printf("The string is not symmetric.\n");
return 0;
}
```
如果你还有其他问题,可以问我。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)