PDA

View Full Version : تابع بازگشتی برای تشخیص رشته متقارن



vb_nima
یک شنبه 01 اردیبهشت 1387, 22:15 عصر
سلام.
دوستان کدی میخواستم که بتونه با یک تابع بازگشتی یک رشته متقارن را تشخیص دهد .فاصله ها را در نظر نگیرد.
خودم فکر میکنم با تابع بازگشتی حروف رشته را بر عکس میکنیم اگر با اصلش یکی بود اونوقت متقارن.ولی نتونستم پیاده سازیش کنم.
ممنون.

emad_67
یک شنبه 01 اردیبهشت 1387, 22:31 عصر
اگه محدودیتی در نوشتن برنامه نداری میتونی برای برعکس کردن رشته از تابع strrev استفاده کنی.(در هدر string.h)

radium
چهارشنبه 04 اردیبهشت 1387, 17:50 عصر
اینو نوشتم و جواب میده امیدوارم به دردت بخوره.





// mirror.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int check(char*,int ,int );
void main()
{
char s[50];
int l,low,high,r;
cout<<"\nEnter string: ";
cin.getline(s,49);
l=strlen(s);
if (l==1)
{
cout<<"\nFaghat 1 harf ast";
exit(0);
}
high=l-1;
low=0;
r=check(s,low,high);
if(!r)
cout<<"\n Namotegharen\n";
else
cout<<"\n Motegharen\n";
}
//###########################################
int check(char* s,int low,int high)
{
if(low>=high)
return 1;
while (s[low]==' ' )
low++;
while(s[high]==' ')
high--;
if(s[low]!=s[high])
return 0;
else
check(s,low+1,high-1);
}