Posts

Showing posts from 2021

Create a Reversed Copy of a String

 A recent question on Stack Overflow concerned creation of a C program to create and print a reverse copy of a string. Thus, for example, the string “hello” would be copied to another string and would contain “olleh”. C versions While this is clearly a beginner level problem, the difficulties encountered by the person submitting the question illustrate how learning C can be a struggle. The source code posted by the author of the question is: #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #define ARR_SIZE 50 int main() { char string[ARR_SIZE]; printf( "Enter char array!\n" ); fgets(string, ARR_SIZE, stdin); string[strlen(string) - 1 ] = '\0' ; int length = (strlen(string) - 1 ); int length2 = (strlen(string) - 1 ); printf( "%s\t%d\n" , string, length); for ( int i = 0 ; i <= length; i++) { printf( "INDEX = %d CHAR = %c\n" , i, string[i]); } printf( ...