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

pls anyone can help me to write a code to print the values in words for any value.Example:1034 to print as "one thousand and thirty four only"

2 Answers  


Sir... please give some important coding questions asked by product companies..

0 Answers  


Write a Program that Inputs 10 Numbers in an Array and Show the Maximum Number

2 Answers   Ace Info,


What is the problem with the following code segment? while ((fgets(receiving array,50,file_ptr)) != EOF) ;

1 Answers  


main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); } int i;

1 Answers  






Given a spherical surface, write bump-mapping procedure to generate the bumpy surface of an orange

0 Answers  


main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); }

7 Answers  


main() { extern int i; i=20; printf("%d",sizeof(i)); }

2 Answers  


why do you use macros? Explain a situation where you had to incorporate macros in your proc report? use a simple instream data example with code ?

0 Answers  


struct Foo { char *pName; }; main() { struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); strcpy(obj->pName,"Your Name"); printf("%s", obj->pName); } a. Your Name b. compile error c. Name d. Runtime error

3 Answers   HCL,


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

2 Answers  


void main() { int k=ret(sizeof(float)); printf("\n here value is %d",++k); } int ret(int ret) { ret += 2.5; return(ret); }

1 Answers  


Categories