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

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  


Write a Program in 'C' To Insert a Unique Number Only. (Hint: Just Like a Primary Key Numbers In Database.) Please Some One Suggest Me a Better Solution for This question ??

0 Answers   Home,


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

1 Answers   HCL,


how to swap 3 nos without using temporary variable

4 Answers   Satyam,


main() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); }

6 Answers  






#define SQR(x) x * x main() { printf("%d", 225/SQR(15)); } a. 1 b. 225 c. 15 d. none of the above

3 Answers   HCL,


union u { union u { int i; int j; }a[10]; int b[10]; }u; main() { printf("\n%d", sizeof(u)); printf(" %d", sizeof(u.a)); // printf("%d", sizeof(u.a[4].i)); } a. 4, 4, 4 b. 40, 4, 4 c. 1, 100, 1 d. 40 400 4

3 Answers   HCL,


main() { int x=5; for(;x!=0;x--) { printf("x=%d\n", x--); } } a. 5, 4, 3, 2,1 b. 4, 3, 2, 1, 0 c. 5, 3, 1 d. none of the above

2 Answers   HCL,


Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.

1 Answers  


In a gymnastic competition, scoring is based on the average of all scores given by the judges excluding the maximum and minimum scores. Let the user input the number of judges, after that, input the scores from the judges. Output the average score. Note: In case, more than two judges give the same score and it happens that score is the maximum or minimum then just eliminate two scores. For example, if the number of judges is 5 and all of them give 10 points each. Then the maximum and minimum score is 10. So the computation would be 10+10+10, this time. The output should be 10 because 30/3 is 10.

0 Answers   TCS,


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  


main() { int i = 0xff ; printf("\n%d", i<<2); } a. 4 b. 512 c. 1020 d. 1024

2 Answers   HCL,


Categories