Thread: I wrote this code in C to reverse string or numbers. need help to shrink it down.

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    31

    I wrote this code in C to reverse string or numbers. need help to shrink it down.

    need help to make it shorter

    I wrote this code in C to reverse string or numbers. need help to shrink it down.-c-png

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,416
    A few suggestions:
    • Do not use a global variable. There is no need for one.
    • Separate input, reversing, and output. You might even write a function for each of them.
    • Note that in your reverse function, you created a variable length array. While this is standard in the 1999 edition of the C standard (C99), it is not guaranteed to be supported post-C99, and wasn't standard pre-C99, even though compilers might have provided it as a language extension.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,429
    > need help to shrink it down.
    Why?

    The number of characters in your source code has nothing to do with how long the code takes to run.

    But what does happen with attempts to make code unnecessarily compact is the introduction of bugs that are hard to find and fix, along with increases in the time it takes to craft the thing in the first place, and the time taken to comprehend WTF is going on at a later time.

    A better algorithm makes code better. Quicksort for example takes more code than bubble sort, but quicksort just blows bubble sort out of the water when it comes to performance.

    Also, when you post code, copy/paste the text between [code][/code] tags. Fuzzy screen shots don't cut it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2018
    Posts
    31
    any other way how to short this code please help im new at C language

    Code:
    #include <stdio.h>                                                                                                             #define MAXLINE 1000 /* maximum input line length */                                                                           
                                                                                                                                   
    int mygetline ( char line[], int maxline );                                                                                    
    int revstringf(char line[], char revline[]);                                                                                   
                                                                                                                                   
    /* revers string using revstringf function */                                                                                  
                                                                                                                                   
    int main()                                                                                                                     
    {                                                                                                                              
        char line[MAXLINE];                                                                                                        
        char revline[MAXLINE];                                                                                                     
                                                                                                                                   
        while ((mygetline(line, MAXLINE)) > 0){                                                                                    
            revstringf(line, revline);                                                                                             
            printf("%s", revline);                                                                                                 
        }                                                                                                                          
        return 0;                                                                                                                  
    }
    
    
    /* mygetline: read a line into s, return length */                                                                             
    int mygetline(char s[], int lim)                                                                                               
    {                                                                                                                              
        int c, i;                                                                                                                  
                                                                                                                                   
        for (i=0; i < lim-1 && (c=getchar() ) !=EOF && c!='\n'; ++i)                                                               
            s[i] = c;                                                                                                              
        if (c == '\n') {                                                                                                           
            s[i] = c;                                                                                                              
            ++i;                                                                                                                   
        }                                                                                                                          
        s[i] = '\0';                                                                                                               
        return i;                                                                                                                  
    }                                                                                                                              
    /* revstringf: reverses string s */                                                                                            
    int revstringf(char s[], char revline[])                                                                                       
    {                                                                                                                              
        int i, ii;                                                                                                                 
                                                                                                                                   
        i = 0;                                                                                                                     
                                                                                                                                   
        while(s[i] != '\n')    /* find number of last character in a string */                                                     
            ++i;                                                                                                                   
        ii = 0;                                                                                                                    
        for(i = i-1; i >= 0; --i){    /* last character of s place as first character of revline and so on */                      
            revline[ii] = s[i];
            ++ii;
        }
        revline[ii] = '\n';
        revline[++ii] = '\0';
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Perhaps you could fill your string from the end and work your way back in mygetline:
    Code:
    s[lim] = '\0';
    for (i=lim - 1; i >= 0 && (c=getchar() ) !=EOF && c!='\n'; --i)
      s[i] = c;
    but then you'll need to move the start address of the string when it is complete.

  6. #6
    Registered User
    Join Date
    May 2016
    Posts
    104
    This is a simpler reversing algorithm. It's a bit shorter and faster too. With the added benefit of not having to pass another buffer to store the reversed line.
    Code:
    void    reverse_line(char *line) {
        unsigned int    length = 0, idx = -1;
        char            temp;
    
    
        while (line[++idx] && line[idx] != '\n');
        length = (idx / 2);
        for (unsigned int i = 0; length; --length, ++i) {
            temp = line[i];
            line[i] = line[--idx];
            line[idx] = temp;
        }
    }
    Testing it out:
    Code:
    int    main(void)
    {
        char line[64];
    
    
        strcpy(line, "123456789\n");
        reverse_line(line);
        printf("%s", line);
        strcpy(line, "ABCDEFGH123456789");
        reverse_line(line);
        printf("%s\n", line);
        return (0);
    }
    Output:
    Code:
    pureuser@pureuser-pc:~/Documents/projects/C/ft_printf$ ./a.out 
    987654321
    987654321HGFEDCBA



  7. #7
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    248
    Quote Originally Posted by erald23 View Post
    I wrote this code in C to reverse string or numbers.
    Sure you did.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,429
    Quote Originally Posted by Sir Galahad View Post
    Nice work detective!.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    248
    Quote Originally Posted by Salem View Post
    Nice work detective!.
    Thanks Chief.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 30
    Last Post: 12-11-2017, 12:26 PM
  2. How to more intuitively wrote more elegant c++ code?
    By Tesp in forum C++ Programming
    Replies: 13
    Last Post: 07-26-2016, 03:58 PM
  3. string reverse. cannot find the bug in my code.
    By getnitin15 in forum C Programming
    Replies: 25
    Last Post: 03-04-2012, 11:11 AM
  4. I wrote a piece of code, can't find where is the bug....
    By meili100 in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2007, 11:25 AM
  5. Problem with my string reverse code
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-24-2005, 09:22 PM

Tags for this Thread