need help to make it shorter
need help to make it shorter
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
> 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.
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; }
Perhaps you could fill your string from the end and work your way back in mygetline:
but then you'll need to move the start address of the string when it is complete.Code:s[lim] = '\0'; for (i=lim - 1; i >= 0 && (c=getchar() ) !=EOF && c!='\n'; --i) s[i] = c;
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.
Testing it out: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; } }
Output: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); }Code:pureuser@pureuser-pc:~/Documents/projects/C/ft_printf$ ./a.out 987654321 987654321HGFEDCBA
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.