#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char a[] = "adarsh";
int len = sizeof(a)/sizeof(a[0]) - 1; // subtract 1 to exclude null terminator
printf("%d\n", len);//scanf only no getline
/*IMP we need to exclode '\0' for reversing so only 6 char "adarsh"*/
for(int i=0; i<len/2; i++){ // corrected loop condition
char temp = a[i];
a[i] = a[len-1-i];
a[len-1-i] = temp;
}
printf("%s\n", a);
return 0;
}
#if 0
int main(){
char *str = NULL;
size_t len = 0;
if (getline(&str, &len, stdin) == -1) {
printf("no input");
return 1;
}
// Remove newline character from input
str[strcspn(str, "\n")] = '\0';
// Reverse the string
int str_len = strlen(str);
for(int i=0;i<str_len/2;i++){
char tmp = str[i];
str[i] = str[str_len-i-1];
str[str_len-i-1] = tmp;
}
printf("%s", str);
// Free dynamically allocated memory
free(str);
str = NULL;// pointer to NULL after freeing it, we ensure that any further
//attempts to use the pointer will cause a segmentation fault, which is easier
//to debug and less likely to cause harm.
}
#endif
To embed this program on your website, copy the following code and paste it into your website's HTML: