strings in reverse order

This is a discussion on strings in reverse order within the C Programming forums, part of the General Programming Boards category; would you say these loops are correct? if not could anyone tell me why or give me any hints the ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    36

    strings in reverse order

    would you say these loops are correct? if not could anyone tell me why or give me any hints

    the question is.... Write two loops to display the characters of the string s in reverse order. One should use array notation, and one pointer notation.

    array
    Code:
    char s[100];
    int i;
    for( i=99; i==0; i--)
    printf(“%c”, s[i]);
    pointer
    Code:
    char *sp;
    sp=&s[99];
    do { printf("%c", *sp);
             sp--;   }
    while (sp != s);

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> would you say these loops are correct?

    What output do you get? Does it look correct to you?
    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;
    }

  3. #3
    Always learning
    Join Date
    Apr 2009
    Posts
    25
    The first one is wrong with only a first glance. Evaluate it step by step. What is the mistake?

    The second one is almost correct. Tip: You know that in C strings can be shorter than the array's size. What terminates a string is the null character '\0'.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about...
    Code:
    int O_0( char *O_O )
    {
        if( (O_O) && *(O_O) )
        {
            (O_0)( O_O + 1 );
            putchar( *(O_O) );
        }
        return (0x0);
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Check your for loop

    Code:
    for( i=99; i==0; i--)
    What does it say, start the index at 99 and cheap decrementing the index only if index is 0. Does that make any sense? NO

    What you need is index should be decrementing until i >= 0 . Make sense ???

    -ssharish
    Last edited by ssharish2005; 04-14-2009 at 05:40 PM.
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    Code:
    void revprint(const char *s)
    {
      const char *end = s;
    
      for(;*end;++end);
      while(--end >= s)
        putchar(*end);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print Array in reverse order
    By swgh in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2007, 01:41 PM
  2. Help with strings
    By andy84 in forum C Programming
    Replies: 8
    Last Post: 12-01-2004, 03:15 PM
  3. what is the significance of low order and high order bits
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 11:46 AM
  4. Printing String in reverse order
    By ling in forum C Programming
    Replies: 14
    Last Post: 10-18-2001, 10:03 AM
  5. read into array and spit out in reverse order
    By steven in forum C Programming
    Replies: 4
    Last Post: 09-07-2001, 03:27 PM

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