hey dis is ma code for srting reversal
which reveres da string like"hey i'm danish" to "danish i'm hey"
jst chek da code
Code:#include <conio.h> #include <stdio.h> #include <string.h> void rev_str (char str[],char out_str[],int len); void substitute (char temp[],char str[]); void rev(char str[],int len); void main () { clrscr (); char in_str[100],out_str[100]; int len; printf ("Enter a string:- "); gets(in_str); printf("\nEntered string is:- "); puts (in_str); len=strlen(in_str); out_str[0]='\0'; rev_str(in_str,out_str,len); printf("\n\nReversed String is:-"); puts (out_str); getch (); } void rev(char str[],int len) { char temp[50]; int i,j; for (i=0,j=len-1; i<len,j>=0; i++,j--) temp[i]=str[j]; temp[i]='\0'; strcpy(str,temp); } void substitute (char temp[],char str[]) { int temp_len=strlen(temp); int i; int str_len=strlen(str); rev(temp,temp_len); for (i=0;i<temp_len;i++) str[i+str_len]=temp[i]; } void rev_str (char in_str[],char out_str[],int len) { char temp[50]=""; int i=0,j=0; for (i=len-1;i>=0;i--) { if(in_str[i]==' ') { substitute (temp,out_str); j=-1; for (int k=0;k<strlen(temp);k++) temp[k]='\0'; } else temp[j] = in_str[i]; j++; } substitute (temp,out_str); // out_str[len+1]='\0'; }
Last edited by WingedPanther; 03-22-2010 at 05:44 AM. Reason: add code tags (the # button)
Are you saying you don't think it works right? What tests have you done?
well itz nt givin da appropriate results
Please be more specific: what did you enter, and what does it generate?
Is English a second language for you? I surly hope so because you are writing in chartroom talk. Try not being so lazy and typing out the words in full English. People won't take you very seriously with all that chatroom language which is difficult to read.
Your code doesn't even compile on my computer. The highlighted part is what GCC chokes on; you need a logical operator there, eitherCode:for (i=0,j=len-1; i<len,j>=0; i++,j--)
This is most likely your problem. I'm not surprised the Microsoft compiler let that through, though.Code:for (i=0,j=len-1; (i<len)&&(j>=0); i++,j--) or for (i=0,j=len-1;(i<len)||(j>=0); i++,j--)
By the way...using sscanf() would make your life much easier.
Last edited by dargueta; 03-22-2010 at 12:10 PM. Reason: Formatting
sudo rm -rf /
enter a string:- dani mani
entered string is:- dani mani
reversed string is:- maniü daniÄ┬ï&☺ëF▐3÷Φ╛dani Ü
this is result which i get after compiling the program
and is not the requried one
so should i do now
Did you try both logical operators I suggested?
sudo rm -rf /
Strings: Reversal - C Code Snippet
D'oh! That one is for character-by-character reversal.
yes i tried that but it doesn't work
so any suggestions for me
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks