Category Archives: C Programs

C Programs

C Program to Compare two Strings

C Program to Compare Two Strings

In this tutorial, we will learn to create a C program that will Compare Two Strings in C programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic of C programming.
  • For loop and its concepts in C Programming.
  • Conditional Statements in C Programming.

Algorithm:-

What is a string?

As we all know, the string is a collection of characters, symbols, digits, and blank spaces. In strings, only one variable is declared which can store multiple values.

Compare Two Strings 

First will take the elements of string 1 from the user. Then will take the elements of string 2 from the user. Then will compare the two taken strings without using the predefined functions. i.e. compare them without using the strcmp function.

With the help of this program, we can Compare Two Strings.

Program code:-

Output:-

In the above program, we have first initialized the required variable.

  • string1[100] = it will hold the string value.
  • string2[100] = it will hold the string value.
  • i = it will hod the integer value that will control the for loop

Taking Input string 1 from the user.

Taking Input string 2 from the user.

Printing Output compared string.

C Program to Concatenate Two Strings

C Program to Concatenate Two Strings

In this tutorial, we will learn to create a C program that will Concatenate Two Strings in C programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic C programming.
  • Using Strings in C Programming.

What is a string?

As we all know, the string is a collection of characters, symbols, digits, and blank spaces. In strings, only one variable is declared which can store multiple values.

What is Concatenation?

Concatenation means to join two different words i.e. multiple Strings into a single string value.

Algorithm:-

Program to Concatenate Two Strings:-

First will take the input string one from the user. Then we will take a second-string from the user and now we will concatenate that two stings. And at last, will print the resultant string.

Using the below program example.

Program Code:-

Output:-

In the above program, we have first initialized the required variable.

  • string1[20] = it will hold the string value.
  • string2[20] = it will hold the string value.
  • i = it will hold the integer value.
  • j = it will hold the integer value.
  • *str1 = pointer type variable 1.
  • *str2 = pointer type variable 2.

Input strings from the user.

While loop body for concatenating the strings using the pointers and printing the concatenated string.

C Program to Copy String

C Program to Copy String

In this tutorial, we will learn to create a C program that will Copy String in C programming.

Prerequisites

Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic C programming.
  • Using Strings in C Programming.

What is a string?

As we all know, the string is a collection of characters, symbols, digits, and blank spaces. In strings, only one variable is declared which can store multiple values.

Algorithm:-

Program to Copy String 

First, will take the input string from the user. And then will copy the string. The C programming language has many pre-defined functions for string manipulation. In today’s tutorial, we will do copy work with the help of a strcpy() function. It is used for copying the string from one variable to another variable.

With the help of the below program, we can Copy String.

Program Code:-

Output:-

In the above program, we have first initialized the required variable.

  • var1 = it will hold the string value.
  • var2 = it will hold the string value.

Taking input string from the user for copying the string.

Copying the string with the help of the assignment operator.

Printing the output copied string1 and string2.

C Program to Print Smiling face

C Program to Print Smiling face

In this tutorial, we will learn to create a C program that will Print Smiling face in C programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic C programming.
  • For loop in C programming.

Program to Print Smiling face

As we all know the String is a collection of character data types. In strings, only one variable is declared which can store multiple values. First, we will take the input number from the user for a number of smiles. Then we will Print the Smiling face using the for loop. The ASCII value of 1 is smile.

With the help of this program, we can Print a Smiling face

Algorithm:-

 

Program to Print Smiling face:-

 

Output:-

In the above program, we have first initialized the required variable.

  • smile= It will hold the integer value 1 which is a smile in ASCII.
  • i = It will hold the integer value for controlling the for loop.
  • limit = It will hold the integer value for the number of smiles.

Taking the input number of smiles.

Printing the output smile.

C Program to Swap Two Strings

C Program to Swap Two Strings

In this tutorial, we will learn to create a C program that will Swap Two Strings in C programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic C programming.
  • Conditional statements in C programming.
  • String functions of c programming.

Program to Swap Two Strings 

As we all know the String is a collection of character data types. In strings, only one variable is declared which can store multiple values. First, we will take the input string from the user. Then we will Swap Two Strings using the third variable that will help to swap.

With the help of this program, we can Swap Two Strings.

Algorithm:-

 

Program to Swap Two Strings:-

 

Output:-

In the above program, we have first initialized the required variable.

  • first[100] = it will hold the string value.
  • second[100] = it will hold the string value.
  • flag[100] = it will hold the integer value.

Taking the input string 1 from the user.

Taking the input string 2 from the user.
Print string before the swap.
Swapping the strings,
Printing the output.

C Program to Write Content to File

C Program to Write Content to File

In this tutorial, you will learn about the C Program to Write Content to File with a practical example.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic C programming.
  • Conditional Statements in C programming.
  • Using file functions of c language.

Program to Write Content to File.

As we all know the file is a collection of characters, integers, and many data types. In strings, only one variable is declared which can store multiple values. First will take the file from the user. Then will Check that file if it’s empty or not. The C programming language has many pre-defined functions for file manipulation. but in today’s tutorial, Write Content to File from the user.

With the help of the below program, we can Write Content to File.

Algorithm:-

Program to Write Content to File:-

 

Output:-

In the above program, we have first initialized the required variable.

  • *fptr  = it will hold the address value.
  • sentence[1000] = it will hold the string data.

opening the file in write mod.

If the pointer is null then exit.

Taking the input sting from the user.

Closing the file.

 

C Program to Read a File

C Program to Read File

In this tutorial, you will learn about the C Program to Read File with a practical example.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic C programming.
  • Concepts of while loop.
  • Conditional Statements in C programming.
  • Using file functions of c language.

Program to reading a file.

As we all know the file is a collection of characters, integers, and many data types. In strings, only one variable is declared which can store multiple values. First will take the file from the user. Then will Check that file if it’s empty or not. The C programming language has many pre-defined functions for file manipulation. but in today’s tutorial,  Read the File from the user.

With the help of the below program, we can Read a File.

Algorithm:-

Program:-

 

Output:-

In the above program, we have first initialized the required variable.

  • *fp  = it will hold the address value.
  • str[500] = it will hold the string data.
  • fname[20] = it will hold the string data.

Taking input file name.

Opening the file and reading it.

Displaying the data from the file.

 

C Program to Read & Display File

C Program to Read & Display File

In this tutorial, you will learn about the C Program to Read & Display File with a practical example.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic C programming.
  • Concepts of while loop.
  • Conditional Statements in C programming.
  • Using file functions of c language.

Program to reading & Display File

As we all know the file is a collection of characters, integers, and many data types. In strings, only one variable is declared which can store multiple values. First will take the file from the user. Then will Check that file if it’s empty or not. The C programming language has many pre-defined functions for file manipulation. but in today’s tutorial,  Read & Display File from the user.

With the help of the below program, we can Read & Display.

Algorithm:-

Program:-

 

Output:-

In the above program, we have first initialized the required variable.

  • *fp  = it will hold the address value.
  • str[500] = it will hold the string data.

Taking input file name.

Opening the file and reading it.

Displaying the data from the file.

Closing the file.

C Program to Copy a File

C Program to Copy a File

In this tutorial, you will learn about the C Program to Copy a File with a practical example.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic C programming.
  • Concepts of while loop.
  • Conditional Statements in C programming.
  • Using file functions of c language.

Program to Copy a File

As we all know the file is a collection of characters, integers, and many data types. In strings, only one variable is declared which can store multiple values. First will take the file from the user. Then will Check that file if it’s empty or not. The C programming language has many pre-defined functions for file manipulation. but in today’s tutorial, we will Copy a File.

With the help of this program, we can Copy a File

Algorithm:-

Program:-

 

Output:-

In the above program, we have first initialized the required variable.

  • *source  = it will hold the address value.
  • *target = it will hold the new file data.

Taking input file name.

Reading the file and taking the target file.

Copying the file.

Printing the output.

C Program to List Files in Directory

C Program to List Files in Directory

In this tutorial, you will learn about the C Program to List Files in Directory with a practical example.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic C programming.
  • Concepts of while loop.
  • Conditional Statements in C programming.
  • Using file functions of c language.

Program to List Files in Directory

As we all know the file is a collection of characters, integers, and many data types. In strings, only one variable is declared which can store multiple values. First will take the file from the user. Then will Check that directory if it’s empty or not. The C programming language has many pre-defined functions for file manipulation. but in today’s tutorial, we will List Files in Directory.

With the help of this program, we can List Files in the Directory

Algorithm:-

Program:-

 

Output:-

In the above program, we have first initialized the required variable.

  • *de  = it will hold the address value.
  • *dr = it will hold the dir data.

Opening and reading the directory.

Reading the directory if empty then.

Generating the list and printing the list of files present in the directory using the while loop.

 

C Program to Encrypt & Decrypt a File

C Program to Encrypt & Decrypt a File

In this tutorial, you will learn about the C Program to Encrypt & Decrypt a File with a practical example.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic C programming.
  • Concepts of while loop.
  • Conditional Statements in C programming.
  • Using file functions of c language.

Program to Encrypt & Decrypt a File

As we all know the file is a collection of characters, integers, and many data types. In strings, only one variable is declared which can store multiple values. First will take the file from the user. Then will encrypt that file so that anyone can not read it directly. The C programming language has many pre-defined functions for file manipulation. but in today’s tutorial, we will Encrypt & Decrypt a File.

With the help of this program, we can Encrypt & Decrypt a File

Algorithm:-

Program:-

 

Output:-

In the above program, we have first initialized the required variable.

  • *fn = it will hold the address value.
  • ch = it will hold the character value.
  • fname[20] = it will hold the string value of the file name.
  • *fne = it will hold the address value.

Taking Input file from the user.

Reading the file.

Encrypting the file.

Printing output.

C Program to Get Input from User

C Program to Get Input from User

In this tutorial, we will learn to create a C program that will Get Input from Users in C programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic C programming.

Get Input from the Users

As we all know the program is a collection of different data types. In c programming, we can take the input from the user in various data types. In today’s program, we will take input in many formats. The second will display the data.

With the help of this program, we can Get Input from User

Algorithm 1:-

Algorithm 2:-

Program to Get Input from User:-

Program 2:-

 

Output:-

In the above program, we have first initialized the required variable.

  • no1 = it will hold the integer value.
  • no2 = it will hold the float value.
  • no3 = it will hod the double value.
  • chr = it will hold the character value.

Taking Input from the user.

 

Printing the data in the output.