hi guys , what i am trying to do is ,to take a word as
input from user and reverse its order like world
into dlrow and tell the user that the word is same or not after reversing its order
Here is the code please check it cause it is not working

#include<stdio.h>
#include<conio.h>
void main()
{
 char name[10]={0,0,0,0,0,0,0,0,0,0};
 char namre[10]={0,0,0,0,0,0,0,0,0,0};
 int i=0,a=0,j,q=0,num=0;
 clrscr();
 printf("Please Enter The Number Of Leters In Your Word");
 scanf("%d",&num);
 printf("enter\n");
 for(i=0;i<num;i++)
  {
  name[i]=getche();
  a++;
  }
  printf("\n");
 q=a;
 for(j=0;j<a;j++)
  {
  q--;
  namre[q]=name[j];
   if(namre[q]==name[j])
   {
   printf("the word is same");
   break;
   }
   else
   {
   printf("the word is not same");
   break;
   }
  }
 getch();
}

Recommended Answers

All 10 Replies

Where is it not working? If ithere are compile errors, give us the error messages. At first glance getche(); should be changed to getchar();

And you should use int main() and end the programm with return 0;

Many problems in your code to begin with:

  1. Dont use conio.h its a non standard header file ( you seem to be using Turbo C )
  2. Dont use clrscr() to clear the screen. Its a non portable function which works only on Turbo C. Use it at your own risk
  3. What you are trying to do is called "finding whether a string is Palindrome" i.e. if a word both in its original form and reversed is the same. eg. deed, civic
  4. Are you sure you want to accpet the input character by character ? Because normally input is accpeted in form of strings.

If you want in string form then consider something like:

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

void remove_newline( char* input )
{
    char* p = 0 ;
    if( p = strrchr( input, '\n' ) )
        *p = '\0' ;
}

int main( void )
{
    int i = 0 , j = 0;
    char buffer[BUFSIZ] = {'\0'} ;

    printf( "Enter the string: " ) ;
    fgets( buffer, BUFSIZ, stdin ) ;
    remove_newline( buffer ) ;

    int length = strlen( buffer ) ;
    char* my_string = (char*) malloc( length + 1 ) ;

    for( i = 0, j = length - 1; j >= 0; --j, ++i)
    {
        my_string[i] = buffer[j] ;
    }
    my_string[i] = '\0' ;

    printf( "The new string: %s", my_string ) ;
}

Though its a lengthy way, but is simple to understand for a beginner.
Make the required changes if you want to accept the user input in form of single characters. If any problems then repost your doubts.

Hope it helped, bye.

thanks for ur help
yes i want to check wethere word Palindrome
or not
I am using Turbo C ,what should i use instead of clrscr();

Where is it not working? If ithere are compile errors, give us the error messages. At first glance getche(); should be changed to getchar();

no there r no errors
my if condition is not working properly

my if condition is not working properly

That is because your logic is wrong.

for(j=0;j<a;j++)
    {
        q--;
        namre[q]=name[j];
    }
    if( strncmp(namre,name,num ) == 0)
    {
        printf("the word is same");
    }
    else
    {
        printf("the word is not same");
    }

Use #include <string.h> at the beginning of your code.

thanks

char *reverse(char *ptr);
char buffer[100];
main()
{
char *list, *ptr,*p = "c is better than pascal";
char *str="", test[3] = "rah";
list = strtok(p, " ");
list = reverse(list);
strcat(str,"tes");
printf("%s \n", buffer);
}

char * reverse(char *ptr) {
if (ptr) {
reverse(strtok(NULL, " "));
strcat(buffer,ptr);
strcat(buffer, " ");
}
}

commented: Bad code and 7 years too late! -3
commented: Type the code for reverse methode not strrev(). +0

I have edited your code....
I do not have a compiler.. so i haven't been able to test it....

#include<stdio.h>
#include<conio.h>
void main()
{
	char name[10]={0,0,0,0,0,0,0,0,0,0};
	char namre[10]={0,0,0,0,0,0,0,0,0,0};
	int i=0,a=0,j,q=0,num=0;
	int flag =0;
	clrscr();
	printf("Please Enter The Number Of Leters In Your Word");
	scanf("%d",&num);
	printf("enter\n");
	for(i=0;i<num;i++)
	{
		name[i]=getche();
		a++;
	}
	printf("\n");
	q=a-1; //if your array length is 9 then array will start as 0,1, .... 8
	for(j=0;j<a;j++)
	{
		q--;
		if(name[q]!=name[j])
		{
			flag=1; //if the reverse is not the same then you are updating flag to 1 and breaking from the loop 
			break;
		}
	}
	if(flag==0)
		printf("the word is same");
	else
		printf("the word is not same");
	getch();
}

try it out....
and let me know if you need further explanation....

#include<stdio.h>
#include<conio.h>
void main()
{
	char name[10]={0,0,0,0,0,0,0,0,0,0};
	char namre[10]={0,0,0,0,0,0,0,0,0,0};
	int i=0,a=0,j,q=0,num=0;
	int flag =0;
	clrscr();
	printf("Please Enter The Number Of Leters In Your Word");
	scanf("%d",&num);
	printf("enter\n");
	for(i=0;i<num;i++)
	{
		name[i]=getche();
		a++;
	}
	printf("\n");
	q=a; //did a small mistake here...
	for(j=0;j<a;j++)
	{
		q--;
		if(name[q]!=name[j])
		{
			flag=1; //if the reverse is not the same then you are updating flag to 1 and breaking from the loop 
			break;
		}
	}
	if(flag==0)
		printf("the word is same");
	else
		printf("the word is not same");
	getch();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.