Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions string.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//Program to reverse a string without using library function

#include <stdio.h>

int main() {
char str[100];
char rev[100];
int i, length = 0;

// Input string from user
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);

//Display the entered string
printf("You entered: %s", str);

// Calculate length of string
for (i = 0; str[i] != '\0'; i++) {
length++;
}

// Adjust length if newline character is present
if (length > 0 && str[length - 1] == '\n') {
length--;
}

// Reverse the string
for (i = 0; i < length; i++) {
rev[i] = str[length - 1 - i];
}

//Display the reversed string
rev[length] = '\0'; // Null-terminate the reversed string
printf("Reversed string is: %s\n", rev);
return 0;
}