HDT_2029 Palindromes _easy version

本文介绍了一种使用C++编程语言判断一个字符串是否为回文串的方法。通过定义两个指针,一个从字符串头部开始,另一个从尾部开始,并逐步向中间靠拢比较字符,实现了高效判断。

这题是求"回文串",

所谓"回文"就是一个字符串正反读来都是一样的,

例如:level, noon.

在一个for循环里面定义了两个变量:i,j,

以前没有用过这种方法,也不知道可不可以用,

但事实证明还是可以用的,呵呵

以下是代码:

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

using namespace std;

int main()
{
	int n, x, i, j, flag;
	char str[100];
	while (cin >> n)
	{
		for (x = 0; x < n; x ++)
		{
			cin >> str;
			flag = 0;
			for (i = 0, j = strlen(str) - 1; i < strlen(str), j >= 0; i ++, j --)
			{
				if (str[i] == str[j])
					flag = 1;
				else 
				{
					flag = 0;
					break;
				}
			}
		
			if (flag == 1)
				cout << "yes" << endl;
			else cout << "no" << endl;
		}
	}
	
	system ("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值