How to reverse a String without using C functions ?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
how to test pierrot divisor
Code for 1>"ascii to string" 2>"string to ascii"
1 Answers Aricent, Global Logic,
#define prod(a,b) a*b main() { int x=3,y=4; printf("%d",prod(x+2,y-1)); }
#define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr, int)); }
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); }
How we print the table of 2 using for loop in c programing?
How do you write a program which produces its own source code as its output?
struct Foo { char *pName; char *pAddress; }; main() { struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); obj->pName = malloc(100); obj->pAddress = malloc(100); strcpy(obj->pName,"Your Name"); strcpy(obj->pAddress, "Your Address"); free(obj); printf("%s", obj->pName); printf("%s", obj->pAddress); } a. Your Name, Your Address b. Your Address, Your Address c. Your Name Your Name d. None of the above
How to swap two variables, without using third variable ?
104 Answers AB, ADP, BirlaSoft, Cisco, Cygnet Infotech, HCL, Hewitt, Honeywell, HP, IBM, Infosys, Manhattan, Microsoft, Mobius, Percept, Satyam, SofTMware, TCS, Wipro, Yamaha,
how to print 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 using any loop(for or while) only once(only 1 loop) and maximum 2 variables using C.
19 Answers Cap Gemini, Infosys,
program to Reverse a linked list
12 Answers Aricent, Microsoft, Ness Technologies,
Finding a number which was log of base 2