C Online Compiler
Example: String Reversal using For Loop in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// String Reversal using For Loop #include <stdio.h> #include <string.h> void reverseStringForLoop(char *str) { int length = strlen(str); int i; char temp; for (i = 0; i < length / 2; i++) { // Step 1: Store the character at the current start index temp = str[i]; // Step 2: Replace the character at the start with the character from the end str[i] = str[length - 1 - i]; // Step 3: Replace the character at the end with the stored start character str[length - 1 - i] = temp; } } int main() { char myString[] = "programming"; printf("Original string: %s\n", myString); reverseStringForLoop(myString); printf("Reversed string: %s\n", myString); return 0; }
Output
Clear
ADVERTISEMENTS