C Program to reverse a String with and without strrev() Function

In this C program, We will show “How to reverse a String with and without strrev() Function ” . So, let’s do it …

With strrev() Function

#include<stdio.h>
int main()
{
char str1[] = "Nazmul Hasan";
printf("%s\n",str1);

strrev(str1);

printf("%s\n",str1);

return 0;
}

Without strrev() Function

#include<stdio.h>
int main()
{
    char A[] = "Nazmul Hasan";
    printf("%s\n",A);

    int i=strlen(A)-1;

    while(A[i]!=0)
    {
        printf("%c",A[i]);
        i--;

    }

    return 0;
}

Or,

#include<stdio.h>
int main()
{
    char A[] = "Progra";
    char B[30];

    strlen(A);
    printf("Length of A = %d\n",strlen(A));

    int d = strlen(A);
    int i=d-1, j=0;

    while(i>=0,j<d)
          {
              B[j] = A[i];
              i--;
              j++;

          }
    B[j] = '\n';

    printf("String1 = %s\n",A);
    printf("String2 = %s",B);

return 0;
}

Keep Learning with PrologiCode.

Leave a Comment