سلام بر همگی
غدير خم و ولادت عيسي بن مريم برهمه بني آدميان مبارك باد.....
دوستان من یه برنامه ای میخوام که تشخیص بده ، کلمه ی وارد شده palindrome است یا نه ؟! (palindrome یعنی کلمه هایی مثل گرگ ، کیک ، kavak و...) البته نتونستم بنویسم ، البته چند موردی رو پیدا کردم که با function نوتشته شدن ، کارم میکنن ، ایرادی ندارن انا رو هم میذارم شاید به درد کسی بخوره !:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int isPalindrome( char *s );
int main ( void )
{
int i = 0;
int ch;
char s[100];
while ((ch = getchar()) != '\n') {
if (isalpha(ch)) {
s[i] = ch;
i++;
}
}
if ( isPalindrome(s) == 1) {
printf("Yes, is a palindrome.\n");
} else {
printf("No, not a palindrome.\n");
}
return 0;
}
int isPalindrome( char *s )
{
int i = strlen(s)-1;
int j = 0;
while (j<=i) {
if(s[j] != s[i]) {
return 0;
}
i--;
j++;
}
return 1;
}
اینم دومی :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char palindrome(char *,int,int);
main(){
char x[50],y[50]; /*I wrote it without using any functions I wasn't previously familiar with*/
int i=0,j,r=0,k,len; /*will try to use the functions when I find time*/
printf("Type your string\n");
gets(x);
len=strlen(x);
for (j=0;j<len;j++){
if (x[j]>='A' && x[j]<='Z') /*turning caps into lowercase*/
y[r++]=x[j]-('A'-'a'); /*making new string stripped of everything but letters*/
if (x[j]>='a' && x[j]<='z') /*did this to simplify slightly, my task wasn't worded very clearly anyway*/
y[r++]=x[j];
}
k=r-1;
if (palindrome (y,i,k)==1)
printf("You've got yourself a palindrome\n");
else if (palindrome (y,i,k)==0) printf ("Not a palindrome\n");
getchar();
} /*Works perfectly. Overall, a combination of everything you guys suggested*/
char palindrome(char a[],int i,int k){
if (i>=k) return 1;
else if (a[i]!=a[k]) return 0;
else if (a[i]==a[k]) return palindrome(a,i+1,k-1);
else return 0;
}
ولی من بدون تابع میخوام .... لطفا راهنمایی کنین
با سپاس........