Print Address Using String


Explanation

Strings can be combined together to create a complete sentence or information. In C language, the strcat() function is used to join multiple strings, and the strcpy() function is used to copy one string into another.

In this program, the user enters the city name, state name, and country name. These three strings are combined to form a complete address. The final address is converted into capital letters using the strupr() function and displayed.

How It Works?

  • Step 1: Declare character arrays for city, state, country, and address.
  • Step 2: Accept city, state, and country names from the user.
  • Step 3: Use strcat() to join the city, state, and country strings.
  • Step 4: Copy the final combined string into the address variable using strcpy().
  • Step 5: Convert the address into capital letters using strupr().
  • Step 6: Display the complete address.

Example Program


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

main()
{

    char city[20], state[20], country[20], address[60];

    clrscr();

    printf("Enter the City Name : ");

    scanf("%s", city);

    printf("\nEnter the State Name : ");

    scanf("%s", state);

    printf("\nEnter the Country Name : ");

    scanf("%s", country);

    strcat(city, " ");

    strcat(city, state);

    strcat(city, " ");

    strcat(city, country);

    strcpy(address, city);

    printf("\nAddress is %s", strupr(address));

    getch();

}

Output

Enter the City Name : Pune

Enter the State Name : Maharashtra

Enter the Country Name : India

Address is PUNE MAHARASHTRA INDIA

Practice Exercises

Task 1: Write a C program to accept House Name, City Name, and Country Name from the user and combine them to display a complete address using string functions.

Goal: Learn how to use strcat(), strcpy(), and strupr() functions to join and format strings.

💡 Hint: Declare character arrays to store house name, city name, country name, and complete address. Use strcat() to combine strings and strcpy() to store the final address. Convert the address into capital letters using strupr().
💡 Show Solution

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

main()
{

    char house[20], city[20], country[20], address[60];

    clrscr();

    printf("Enter House Name : ");

    scanf("%s", house);

    printf("\nEnter City Name : ");

    scanf("%s", city);

    printf("\nEnter Country Name : ");

    scanf("%s", country);

    strcat(house, " ");

    strcat(house, city);

    strcat(house, " ");

    strcat(house, country);

    strcpy(address, house);

    printf("\nAddress is %s", strupr(address));

    getch();

}

Output
Enter House Name : GreenVilla

Enter City Name : Mumbai

Enter Country Name : India

Address is GREENVILLA MUMBAI INDIA
Explanation: The program accepts three different strings and combines them using the strcat() function. The complete address is copied into another string using strcpy() and displayed in capital letters using strupr().