reverse string

This is a discussion on reverse string within the C Programming forums, part of the General Programming Boards category; halo all, i've made a program that reverses the input string.but the output is not coming right.here's my code. Code: ...

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    reverse string

    halo all,
    i've made a program that reverses the input string.but the output is not coming right.here's my code.
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    int main(void)
    {
    	char s[100];
    	gets(s);
    	int i;
    	int len=strlen(s);
    	
    	for(i=0;s[i]!='\0';i++)
    		s[i]=s[--len];
    	puts(s);
    getch();
    return 0;
    }
    suppose i input "when"
    the output comes out to be "neen" i.e it doesn't reverses the whole string.
    Thanks

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    That's because you're creating a mirror image of the last half of the string (once you've reached the midpoint, the first half has been overwritten)! You should be swapping elements instead.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 10:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21