String Formatting Function


Explanation

String formatting functions are used to modify and display strings in different formats. In C language, different string functions are available to perform operations like converting a string into uppercase, lowercase, reversing a string, and finding the length of a string.

In this program, the user enters a name. The program uses string functions like strupr(), strlwr(), strrev(), and strlen() to perform different operations on the entered string.

How It Works?

  • Step 1: Declare a character array Name[20] to store the string.
  • Step 2: Accept a name from the user using scanf().
  • Step 3: Display the original name using %s.
  • Step 4: Convert the name into capital letters using strupr().
  • Step 5: Convert the name into small letters using strlwr().
  • Step 6: Reverse the name using strrev().
  • Step 7: Find the length of the name using strlen().

Example Program


#include <stdio.h>
#include <conio.h>
#include <string.h>

main()
{

    char Name[20];

    clrscr();

    printf("Enter the Name : ");

    scanf("%s", Name);

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

    printf("\n Name in Capital %s", strupr(Name));

    printf("\n Name in Small %s", strlwr(Name));

    printf("\n Name in Reverse %s", strrev(Name));

    printf("\n Name's length %d", strlen(Name));

    getch();

}

Output

Enter the Name : Hamid

Name Hamid
Name in Capital HAMID
Name in Small hamid
Name in Reverse dimaH
Name's length 5

Practice Exercises

Task 1: Write a C program to accept a string from the user and display the string in capital letters, small letters, reverse order, and find the length of the string using string functions.

Goal: Learn how to use string formatting functions like strupr(), strlwr(), strrev(), and strlen().

💡 Hint: Declare a character array to store the string. Use strupr() for uppercase conversion, strlwr() for lowercase conversion, strrev() for reversing the string, and strlen() to calculate the string length.
💡 Show Solution

#include <stdio.h>
#include <conio.h>
#include <string.h>

main()
{

    char Name[20];

    clrscr();

    printf("Enter the Name : ");

    scanf("%s", Name);

    printf("\nOriginal Name : %s", Name);

    printf("\nCapital Name : %s", strupr(Name));

    printf("\nSmall Name : %s", strlwr(Name));

    printf("\nReverse Name : %s", strrev(Name));

    printf("\nLength of Name : %d", strlen(Name));

    getch();

}

Output
Enter the Name : Computer

Original Name : Computer
Capital Name : COMPUTER
Small Name : computer
Reverse Name : retupmoC
Length of Name : 8
Explanation: The program accepts a string from the user and applies different string formatting operations. The strupr() function converts the string into uppercase, strlwr() converts it into lowercase, strrev() reverses the string, and strlen() finds the total number of characters in the string.