Reversing a String In C

Reversing a String In C

·

2 min read

This C program defines two functions to reverse a given string:

  1. _strlen(char *str): calculates the length of a string str.

  2. reverse_string(char *str): reverses the string str by swapping its characters.

The main function uses reverse_string to reverse the string "JQK" and prints the result to the console.

Here's a step-by-step breakdown of what the program does:

  1. The _strlen function takes a string str as input and returns its length as an integer. It does this by initializing a variable len to 0, then iterating over the characters in str with a for loop until it reaches the null character ('\0'). For each character, it increments len by 1. Finally, it returns len.

  2. The reverse_string function takes a string str as input and modifies it in place to reverse its characters. It does this by first calling _strlen to determine the length of str. It then initializes two variables, i and j, to the beginning and end of the string, respectively. It then iterates over the string with a for loop, swapping the characters at positions i and j until i and j meet in the middle of the string. It uses a temporary variable temp to hold one of the characters during the swap.

  3. In the main function, a string str containing the characters "JQK" is defined. The reverse_string function is then called with str as its input, which modifies str in place to reverse its characters. Finally, the reversed string is printed to the console using printf.

The output of the program will be:

Reversed string: KQJ