From 3ef88971bc808034504232be3ff04b2c39f7f8fc Mon Sep 17 00:00:00 2001 From: Abhishek Dinkar Raut <9390583+araut1@users.noreply.github.com> Date: Thu, 1 Oct 2020 20:03:40 -0600 Subject: [PATCH] Added Article for Reverse a String in C Issue #226 Add Article for Reverse a String in C. Please let me knwo of any issue. Happy Building! --- .../reverse-a-string/_posts/2020-10-01-c.md | 146 ++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 projects/reverse-a-string/_posts/2020-10-01-c.md diff --git a/projects/reverse-a-string/_posts/2020-10-01-c.md b/projects/reverse-a-string/_posts/2020-10-01-c.md new file mode 100644 index 0000000000..5d452e60ec --- /dev/null +++ b/projects/reverse-a-string/_posts/2020-10-01-c.md @@ -0,0 +1,146 @@ +--- +title: Reverse a String in C +layout: default +last-modified: 2020-10-01 +featured-image: +tags: [c, reverse-a-string] +authors: + - abhishek_raut +--- + +In this article, we will see how to reverse a String in C! + +## How to Implement the Solution + +Below is the whole code for reversing a string in C : + +```c +#include +#include + +void reverseString(char* str) +{ + int len, i; + char *start, *end, temp; + len = strlen(str); + start = str; + end = str; + + for (i = 0; i < len - 1; i++) + { + end++; + } + + while (start + +### Approach + + +Here we create two pointers, one that points to the string's start and the other to the end of the string. Using a third character *variable* as a place holder, we swap the character positions indicated by the pointers. We continue to do so until the entire input string is reversed by moving the *pointers*. + +[Time complexity][1]: O(n) + +
+ +### Breakdown + +Now let us see step-by-step how the code works. + +```c +#include +#include +``` +Here we are including header files (.h files) to use functions like printf(). Header files provided us with tested ready to use functions to ease the software development work. + +By including `#include ` we can use printf() method without worrying about how it is implemented. Can you guess why we have included `#include `? Keep on reading for the answer. + +```c +void reverseString(char* str) { + // code +} +``` +This is the function that will take a string as input and reverse it. Here, we will be modifying the value pointed by *pointer* str. Hence, the function doesn't need to return anything, and the return type of our method is *void*. + +```c + int len, i; + char *start, *end, temp; + len = strlen(str); + start = str; + end = str; +``` +Here we are declaring *variables* to store the length of the string and *pointers* to point start and end points (of the logical substring) to be swapped. Note that currently, the `end` pointer is pointing to the start of the string instead of the end of the string. We will see how to fix it in the next step. + +And yes, the function `strlen()`, which gives us the length of string, can be used because of the statement: `#include `. + +```c + for (i = 0; i < len - 1; i++) + { + end++; + } +``` +We traverse through the entire string so that *pointer* `end` can point to the last character of the string. It's important to note that *for loop* traverse till `len-1` this is because, in C, arrays are zero-indexed i.e., indexing starts from 0. The `++` syntax at the end of the *variable* `end` is called an Increment operator. The increment operator increments the value of the variable by one in each iteration. Thus, after *for loop* is executed, *variable* `end` points to the string's last character. + +```c + while (start