How to reverse a String without using C functions ?

Answers were Sorted based on User's Feedback



How to reverse a String without using C functions ?..

Answer / abhishek joshi ( h7 )

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 ?    133 Yes 37 No

How to reverse a String without using C functions ?..

Answer / raghuram.a

#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 ?    106 Yes 51 No

How to reverse a String without using C functions ?..

Answer / guest

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 ?    104 Yes 63 No

How to reverse a String without using C functions ?..

Answer / shruti

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 ?    70 Yes 32 No

How to reverse a String without using C functions ?..

Answer / atul kabra

#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 ?    47 Yes 25 No

How to reverse a String without using C functions ?..

Answer / shruti

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 ?    59 Yes 40 No

How to reverse a String without using C functions ?..

Answer / sindhuja marri

#include<stdio.h>
#incliude<conio.h>
void main()
{
char a[20]="sindhu";
int i,j,count=0;
printf("the string to be reversed is");
for(i=0;a[i]!='\0';i++)
{
printf("%c",a[i]);
count++;
}

Printf(The reverse string is");
for(j=count;j>=0;j--)
{
printf("%c",a[j]);
}
}

Is This Answer Correct ?    9 Yes 0 No

How to reverse a String without using C functions ?..

Answer / amaresh ch das

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 ?    142 Yes 136 No

How to reverse a String without using C functions ?..

Answer / manoj

#include <stdio.h>


void reverse(char s[])
{
int low; /* index in the lower half of the array s */
int high; /* index in the upper half of the array s */
char c; /* for holding intermediate strings */
int len; /* the length of the string s */

/* Initialize len to the length of the string */
for (len=0;s[len]!='\0';len++) ;

/* Let low increase and high decrease until they meet */
low = 0;
high = len-1;
while (low<high) {
/* Switch the values of s[low] and s[high] */
c = s[low];
s[low] = s[high];
s[high] = c;
low++;
high--;
}
}


main()
{
int c;
char line[80]; /* array to hold a line of input */
int i; /* to use as an index in the array */

c = getchar();
i = 0;

/* Read input until EOF (CTRL-z) */
while (c != EOF) {
if (c != '\n') {
/* Put anything else but newline in the array */
line[i] = c;
i++;
}

if (c == '\n') {
/* For a newline, reverse and print the line and start new line */
line[i] = '\0';
reverse(line);
printf("%s",line);
putchar(c);
i = 0;
}

c = getchar();
}
}

Is This Answer Correct ?    8 Yes 2 No

How to reverse a String without using C functions ?..

Answer / imran silawat

#include<stdio.h>
#include<conio.h>
main()
{
char str[50],revstr[50];
int i=0,j=0;
printf("Enter the string to be reversed");
scanf("%s",str);
for(i=strlen(str)-1;i>=0;i--)
{
revstr[i]=str[i];
i++;
}
revstr[i]='\0';
printf("Input string : %s",str);
printf("\nOutput String : %s",revstr);
getch();
}

Is This Answer Correct ?    11 Yes 5 No

Post New Answer

More C Code Interview Questions

A program that will create a movie seat reservation. The program will display the summary seats and its status. The user will be ask what seat no. to be reserved, then it will go back again to the summary to display the updated seat status. If the seat no. is already reserved then it will prompt an error message. And also if the input seat no is out of range then it will also prompt an error message. The program is continuously running. Termination of the program will depends on how the programmer will apply. Sample output: Movie Seats Reservation Summary of Seats: Seat 1: Available Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 1 Movie Seats Reservation Summary of Seats: Seat 1: Reserve Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 6 The Seat no. is out of range! Movie Seats Reservation Summary of Seats: Seat 1: Reserve Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 1 The Seat no. is already reserved! Movie Seats Reservation Summary of Seats: Seat 1: Reserve Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 0 GoodBye... Thank You!!!

0 Answers  


void main() { printf(“sizeof (void *) = %d \n“, sizeof( void *)); printf(“sizeof (int *) = %d \n”, sizeof(int *)); printf(“sizeof (double *) = %d \n”, sizeof(double *)); printf(“sizeof(struct unknown *) = %d \n”, sizeof(struct unknown *)); }

1 Answers  


how can i cast a char type array to an int type array

2 Answers  


write a c-program to find gcd using recursive functions

5 Answers   HTC, Infotech,


Can you send Code for Run Length Encoding Of BMP Image in C Language in linux(i.e Compression and Decompression) ?

0 Answers   Honeywell,






main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); }

1 Answers   Zoho,


main() { int y; scanf("%d",&y); // input given is 2000 if( (y%4==0 && y%100 != 0) || y%100 == 0 ) printf("%d is a leap year"); else printf("%d is not a leap year"); }

1 Answers  


#ifdef something int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing); }

1 Answers  


Sorting entire link list using selection sort and insertion sort and calculating their time complexity

1 Answers   Infosys, Microsoft, NetApp,


#include<stdio.h> main() { register i=5; char j[]= "hello"; printf("%s %d",j,i); }

2 Answers  


What is the output of the program given below main() { signed char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

1 Answers  


prog. to produce 1 2 3 4 5 6 7 8 9 10

4 Answers   TCS,


Categories