c_cpp C程序读取字符串并检查它是否是回文,而不使用库函数。显示结果。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp C程序读取字符串并检查它是否是回文,而不使用库函数。显示结果。相关的知识,希望对你有一定的参考价值。

/*
* C Program to Check whether a given String is Palindrome or not
* using Recursion
*/

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

void check(char[], int);

int main() {

	char string[25];

	printf("enter a string:\n");
	scanf("%s", string);

	check(string, 0);

	return 0;
}

void check(char word[], int index) {

	int len = strlen(word) - (index + 1);

	if (word[index] == word[len]) {
		if (index + 1 == len || index == len) {
			printf("The entered word is a palindrome.\n");
			return;
		}

		check(word, index + 1);
	}

	else {
		printf("The entered word is not a palindrome.");
	}
}
/*
* C program to read a string and check if it's a palindrome, without
* using library functions. Display the result.
*/
#include <stdio.h>
#include <string.h>

int main() {

	char string[25],
		reverse_string[25] = { '\0' };
	int flag = 0,
		i,
		length = 0;

	fflush(stdin);
	printf("enter string...\n");
	gets_s(string);

	for (i = 0; string[i] != '\0'; i++) {
		length++;
	}

	//hello = 6 | start form 5 | 
	//olleh	


	//reverse the string
	for (i = length - 1; i >= 0; i--) {
		reverse_string[length - i - 1] = string[i];
	}

	for (i = 0; i < length; i++) {
		if (reverse_string[i] == string[i]) {
			flag = 1;
		}
		else {
			flag = 0;
		}
	}


	if (flag == 1) {
		printf("%s is a palindrome\n", string);
	}
	else {
		printf("%s is not a palindrome\n", string);
	}
	

	return 0;
}
/*
* C Program to Check whether a given String is Palindrome or not
* using Recursion
*/

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

void check(char[], int);

int main() {

	char string[25];

	printf("enter a string:\n");
	scanf("%s", string);

	check(string, 0);

	return 0;
}

void check(char word[], int index) {

	int len = strlen(word) - (index + 1);

	if (word[index] == word[len]) {
		if (index + 1 == len || index == len) {
			printf("The entered word is a palindrome.\n");
			return;
		}

		check(word, index + 1);
	}

	else {
		printf("The entered word is not a palindrome.");
	}
}

以上是关于c_cpp C程序读取字符串并检查它是否是回文,而不使用库函数。显示结果。的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp C程序接受两个整数并检查它们是否相等

c_cpp 确定整数是否是回文。这样做没有额外的空间。

7.Deque的应用案例-回文检查

7.Deque的应用案例-回文检查

回文字符串问题:为啥我必须放 +1 而不是 -1 才能使这段代码工作?

C中的程序,它读取声音数据并检查给定数据是不是满足wav文件的先决条件