Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: string reversal

  1. #1
    dans_mahajan is offline Newbie
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Exclamation string reversal

    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)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: string reversal

    Are you saying you don't think it works right? What tests have you done?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    dans_mahajan is offline Newbie
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Re: string reversal

    well itz nt givin da appropriate results

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: string reversal

    Please be more specific: what did you enter, and what does it generate?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    Ancient Dragon's Avatar
    Ancient Dragon is offline Programming Expert
    Join Date
    Jan 2008
    Location
    near St Louis, MO USA
    Posts
    393
    Rep Power
    17

    Re: string reversal

    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.

  7. #6
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: string reversal

    Code:
    for (i=0,j=len-1; i<len,j>=0; i++,j--)
    Your code doesn't even compile on my computer. The highlighted part is what GCC chokes on; you need a logical operator there, either
    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--)
    This is most likely your problem. I'm not surprised the Microsoft compiler let that through, though.

    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 /

  8. #7
    dans_mahajan is offline Newbie
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Re: string reversal

    enter a string:- dani mani
    entered string is:- dani mani

    reversed string is:- mani&#252; dani&#196;┬&#239;&☺&#235;F▐3&#247;Φ╛dani &#220;


    this is result which i get after compiling the program
    and is not the requried one
    so should i do now

  9. #8
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: string reversal

    Did you try both logical operators I suggested?
    sudo rm -rf /

  10. #9
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: string reversal

    Strings: Reversal - C Code Snippet
    D'oh! That one is for character-by-character reversal.

  11. #10
    dans_mahajan is offline Newbie
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Re: string reversal

    yes i tried that but it doesn't work
    so any suggestions for me

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Intermediate Link List Reversal Iterative and Inplace
    By fayyazlodhi in forum C Tutorials
    Replies: 0
    Last Post: 05-29-2011, 12:10 PM
  2. Replies: 11
    Last Post: 03-02-2011, 07:00 PM
  3. Replies: 9
    Last Post: 10-04-2010, 04:37 AM
  4. Java | Converting String to Int & Int to String
    By QuackWare in forum Classes and Code Snippets
    Replies: 0
    Last Post: 01-06-2010, 11:01 AM
  5. adding int to string,creat a file named string..
    By emda321 in forum C and C++
    Replies: 5
    Last Post: 09-10-2009, 07:15 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts