Jump to content

string reversal

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
16 replies to this topic

#1
dans_mahajan

dans_mahajan

    Newbie

  • Members
  • Pip
  • 5 posts
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
#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';
}

Edited by WingedPanther, 22 March 2010 - 04:44 AM.
add code tags (the # button)


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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

#3
dans_mahajan

dans_mahajan

    Newbie

  • Members
  • Pip
  • 5 posts
well itz nt givin da appropriate results

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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

#5
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
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.

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
for (i=0,j=len-1; [COLOR=Red]i<len,j>=0[/COLOR]; 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
for (i=0,j=len-1; [COLOR=Black](i<len)[COLOR=Red]&&[/COLOR](j>=0)[/COLOR]; i++,j--)

or

for (i=0,j=len-1;[COLOR=Black](i<len)[COLOR=Red]||[/COLOR](j>=0[/COLOR]); 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.

Edited by dargueta, 22 March 2010 - 11:10 AM.
Formatting

sudo rm -rf /

#7
dans_mahajan

dans_mahajan

    Newbie

  • Members
  • Pip
  • 5 posts
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

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Did you try both logical operators I suggested?
sudo rm -rf /

#9
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Strings: Reversal - C Code Snippet
D'oh! That one is for character-by-character reversal.

#10
dans_mahajan

dans_mahajan

    Newbie

  • Members
  • Pip
  • 5 posts
yes i tried that but it doesn't work
so any suggestions for me

#11
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Ok, here's my suggestion: Use strtok() to build a list of words, then traverse that list backwards and print the result out.
sudo rm -rf /

#12
dans_mahajan

dans_mahajan

    Newbie

  • Members
  • Pip
  • 5 posts
well i'm using turboC so i don't know how to use that function
i'm not getting where the problem actually lies