954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reverse string arrays

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();
}
saqib
Newbie Poster
16 posts since Sep 2006
Reputation Points: 14
Solved Threads: 0
 

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

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

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

Anonymusius
Posting Whiz in Training
238 posts since Aug 2006
Reputation Points: 129
Solved Threads: 11
 

Many problems in your code to begin with:Dont use conio.h its a non standard header file ( you seem to be using Turbo C )
Dont use clrscr() to clear the screen. Its a non portable function which works only on Turbo C. Use it at your own risk
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
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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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();

saqib
Newbie Poster
16 posts since Sep 2006
Reputation Points: 14
Solved Threads: 0
 
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

saqib
Newbie Poster
16 posts since Sep 2006
Reputation Points: 14
Solved Threads: 0
 
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.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

thanks

saqib
Newbie Poster
16 posts since Sep 2006
Reputation Points: 14
Solved Threads: 0
 

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, " ");
}
}

arungre
Newbie Poster
1 post since Apr 2011
Reputation Points: 7
Solved Threads: 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....

arjunpk
Light Poster
46 posts since Jan 2011
Reputation Points: 4
Solved Threads: 2
 
#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();
}
arjunpk
Light Poster
46 posts since Jan 2011
Reputation Points: 4
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You