ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip       Ask Questions on ANYTHING, that arise in your Daily Life at     FORUM9.COM
Google
 
Categories  >>  Code Snippets  >>  Programming Code  >>  C Code
 
 


 

 
 C Code interview questions  C Code Interview Questions
 C++ Code interview questions  C++ Code Interview Questions
 VC++ Code interview questions  VC++ Code Interview Questions
 Java Code interview questions  Java Code Interview Questions
 Dot Net Code interview questions  Dot Net Code Interview Questions
 Visual Basic Code interview questions  Visual Basic Code Interview Questions
 Programming Code AllOther interview questions  Programming Code AllOther Interview Questions
Question
How to reverse a String without using C functions ?
 Question Submitted By :: Sureshsan
I also faced this Question!!     Rank Answer Posted By  
 
  Re: How to reverse a String without using C functions ?
Answer
# 1
my_strrev(char str[Max]){
    int i; // pointing to base adress
    int l; //pointing to last address strlen(str) -1th position
    char temp;
    for(i=0,l=strlen(str)-1;i<=l; i++ ,j--)
    {
      temp=str[i];
      str[i]=str[l];
      str[l]=temp;
    }
    return str;
}
 
Is This Answer Correct ?    21 Yes 18 No
Amaresh Ch Das
 
  Re: How to reverse a String without using C functions ?
Answer
# 2
char * rev(char * str){
 int temp;  
 for(int j=0;str[j];j++);     
 for(int i=0;i<j;i++,j--){
        temp=str[i];
        str[i]=str[j];
        str[j]=temp;
    } 
 return str;
}
 
Is This Answer Correct ?    20 Yes 6 No
Guest
 
 
 
  Re: How to reverse a String without using C functions ?
Answer
# 3
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
int main()
{
int i=0,l,l1;
char str[100];
cout<<"enter string:";
gets(str);
while(str[i])
i++;
l=i;
for(i=0;i<=(l-1)/2;i++)   //n/2 steps!!no extra memory
{
char t=str[i];
str[i]=str[l-i-1];
str[l-i-1]=t;
}
str[l]=0;
cout<<"\n\nreversed string is:"<<str;
getch();
return 0;
}
 
Is This Answer Correct ?    10 Yes 7 No
Raghuram.A
 
  Re: How to reverse a String without using C functions ?
Answer
# 4
char * rev_string (char * str)
{
char temp;
int i , j;
for (i = 0 ; str[i]!= NULL ; i++);

for(j = 0 ; j < i ; j++ , i--)
{
      temp = str[j];
      str[j] = str[i];
      str[i] = temp;
}

return str;
}
 
Is This Answer Correct ?    6 Yes 4 No
Shruti
 
  Re: How to reverse a String without using C functions ?
Answer
# 5
the above is slightly wrong
this is the corrected one..

char * rev_string (char * str)
{
char temp;
int i , j;
for (i = 0 ; str[i]!= '\0' ; i++);

for(j = 0 ; j < i ; j++ , i--)
{
      temp = str[j];
      str[j] = str[i];
      str[i] = temp;
}

return str;
}
 
Is This Answer Correct ?    14 Yes 1 No
Shruti
 
  Re: How to reverse a String without using C functions ?
Answer
# 6
#include<stdio.h>

void reverse(char *);

void main()
{
	char str[]="Hello";

	reverse(str);
	printf("Reverse String is %s",str);
	
}

void reverse(char *p)
{

	char *q=p;
	while(*++q!='\0');
	q--;

	while(p<q)
	{
		*p=*p+*q;
		*q=*p-*q;
		*p=*p-*q;
		p++;
		q--;
	}

}
 
Is This Answer Correct ?    6 Yes 3 No
Atul Kabra
 
  Re: How to reverse a String without using C functions ?
Answer
# 7
char * rev_string (char * str)
{
char temp;
int i , j;
for (i = 0 ; str[i]!= '\0' ; i++);
i--;

for(j = 0 ; j < i ; j++ , i--)
{
      temp = str[j];
      str[j] = str[i];
      str[i] = temp;
}

return str;
}
 
Is This Answer Correct ?    8 Yes 1 No
Shruti
 
  Re: How to reverse a String without using C functions ?
Answer
# 8
#include<muthu.h>
main()
 {
   printf("\nthe reverse string is:",reversemuthu(s1);
 }

output:
 original string is: muthu
 the reverse string is: uthum
   
Thanks and recgards:
   prof.muthu ph:9962940220
 you can call me any time any where...!
 
Is This Answer Correct ?    3 Yes 8 No
Prof.muthu
 
  Re: How to reverse a String without using C functions ?
Answer
# 9
all these methods are too long.....


HAVE A LOOK ON SIMPLEST AND THE SHORTEST OF ALL 
IN JUST ONE LINE YOU CAN REVERSE ANY STRING
and yes..... without using string.h

CODE IS :



#include<stdio.h>
main()
{
int l,i,j;     /*declaring integer variables*/
char str[10],temp[10];   /* declaring string variables*/

/* Now taking input from the user*/

printf("\n enter any string to reverse =>");
scanf("%s",&str); /* passing into the variable*/

/*finding the lenth of the entered string */
for (l=0;str[l];++l); /*length found;amazing code!isnt it?*/
{printf("\nThis is the length of the string =>%d\n",l);}

/*making loop for reversing the given string*/

j=l;
for (i=0;j>=0;i++,j--)
{
temp[i]=str[j];    /* Note that the string is reversed */
                   /* and saved into new variable i.e temp*/
}

/*Now printing the reversed string*/
printf("\n this is the reversed string =>");

for (i=0;i<=l;i++)
{printf("%c",temp[i]);

/* this loop is calling the characters from the temp variable*/
}
}
/*End of program :-D*/




So this was my coding .....
am abhishek joshi.....
computer and network engineer....
am dot net programmer but also have indepth knowledge of C
and C++
and if you feel this code helpful then at least mention one
thanks vote on my mail id : h7_2007@yahoo.co.in

also if you want such amazing codes of dot net then u can
contact me.....
+919907428052
:-D have a great day.....
 
Is This Answer Correct ?    12 Yes 3 No
Abhishek Joshi ( H7 )
 
  Re: How to reverse a String without using C functions ?
Answer
# 10
int i=0,j=0;
while(str[i++))
{j++;}
i=0;
j--;
while(i<j)
{ char tmp;
  tmp = str[i];
  str[i]=str[j];
  str[j]=tmp;
}
 
Is This Answer Correct ?    2 Yes 1 No
Saurabh
 
  Re: How to reverse a String without using C functions ?
Answer
# 11
#include<stdio.h>
int main()
{
  char str[80],str1[80];
  int i,j;
 printf("\n Enter String:");
 fgets(str,80,stdin);
  for(i=strlen(str)-1,j=0;i>=1;i--)
      str1[j++]=str[i];
 printf("Reversed String is:%s",str1);
}
 
Is This Answer Correct ?    3 Yes 0 No
Yogesh
 
  Re: How to reverse a String without using C functions ?
Answer
# 12
/*The program to reverse the given input string*/

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

void main()
{
   char str[20];
   int i=0,a,b;
   clrscr();

   printf("\nEnter the given string:");
   scanf("%s",str);

   do
   {
	 a=str[i];
	 i++;
   }while(a!=0);

   i=i-1;
   printf("\nThe reverse string:");

   do
   {
	 b=str[i];
	 printf("%c",b);
	 i--;
   }while(i>=0);

   getch();
 
Is This Answer Correct ?    2 Yes 1 No
Sushamaa
 
  Re: How to reverse a String without using C functions ?
Answer
# 13
int l=0,i;
while(str[l]!='\0')
{
l++;
}
for(i=l-1;i>=0;i--)
{
cout<<str{i];
}
 
Is This Answer Correct ?    0 Yes 1 No
Abhishek Gambhir
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
main() { struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }  1
Is the following code legal? struct a { int x; struct a *b; }  1
main() { int i = 257; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }  1
How do you sort a Linked List (singly connected) in O(n) please mail to pawan.10k@gmail.com if u can find an anser...i m desperate to knw... Oracle3
#include<stdio.h> main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d----%d",*p,*q); }  1
Give a one-line C expression to test whether a number is a power of 2. Microsoft8
union u { struct st { int i : 4; int j : 4; int k : 4; int l; }st; int i; }u; main() { u.i = 100; printf("%d, %d, %d",u.i, u.st.i, u.st.l); } a. 4, 4, 0 b. 0, 0, 0 c. 100, 4, 0 d. 40, 4, 0 HCL1
How we print the table of 3 using for loop in c programing?  3
main() { 41printf("%p",main); }8  1
program to Reverse a linked list Ness-Technologies4
Is this code legal? int *ptr; ptr = (int *) 0x400;  1
main() { char *cptr,c; void *vptr,v; c=10; v=0; cptr=&c; vptr=&v; printf("%c%v",c,v); }  1
#define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); }  1
main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); }  1
main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); }  1
main(int argc, char *argv[]) { (main && argc) ? main(argc-1, NULL) : return 0; } a. Runtime error. b. Compile error. Illegal syntax c. Gets into Infinite loop d. None of the above HCL1
How we will connect multiple client ? (without using fork,thread) TelDNA2
write a program in c to merge two array  1
Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it. Microsoft7
To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates. Synergy2
 
For more C Code Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com