Thread: having a bit of trouble

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    17

    having a bit of trouble

    I'm doing an excercise in a book I bought, but I figured I'd go an extra mile. And i'm having a lot of trouble in getting this to work. Here is what I have (the part that works.)

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
            char str[80];
            int i;
    
            printf("Enter a string: ");
            fgets(str, sizeof(str), stdin);
    
            for (i = strlen(str); i>-1; i--)
                    printf("%c", str[i]);
    
            printf("\n");
            return 0;
    
    }
    Now, what I want to be able to do, is to store the 'backwards' string in a seperate array, so that I end up with the 'backwards' string in an entirely different array than str. I've tried various loops, but I can't seem to get it to work :/

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    something like this
    Code:
    char other[80];
    ...
    ...
            for (i = strlen (str); i >= 0; i--,x++){
                    other[x]=str[i];
                    printf ("%c", other[x]);
            }
             x++;
             other[x]='\0';

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Here's one way to do it:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char str[80], strrev[80];
      int i;
      int len;
    
      printf("Enter a string: ");
      fgets(str, sizeof(str), stdin);
      len = strlen(str);
      if(len && str[len-1] == '\n')
        str[--len] = '\0';
    
      for(i = len-1;i > -1;i--)
        strrev[len-i] = str[i];
      strrev[len+1] = '\0';
    
      printf("%s\n", strrev);
      return 0;
    }
    The reason you need to start at len-1 is because str[len] is the null terminating zero that's at the end of every C string. Reversing the string starting with that character would give you your string reversed, but the null terminating zero would be at strrev[0] so when you printed it you wouldn't see anything. It would literally be a backwards string. I hope that makes sense. I'm guessing that's where you were getting stuck in your own attempts. Let me know if you have any questions about that because it's important to understand.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    17
    Reversing the string starting with that character would give you your string reversed, but the null terminating zero would be at strrev[0] so when you printed it you wouldn't see anything.
    That was exactly my problem! Thanks!

    I did:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
            char str[80], backwards[80];
            int i,x=0;
    
            printf("Enter a string: ");
            fgets(str, sizeof(str), stdin);
    
            for (i = (strlen(str) - 1); i>=0; i--,x++)
            {
                    backwards[x]=str[i];
            }
    
            backwards[x] = '\0';
            printf("%s", backwards);
    
            return 0;
    }
    Last edited by principii; 09-15-2004 at 02:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 07:59 AM
  2. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  3. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM