C program to convert lowercase character to uppercase character

How to convert lowercase to uppercase in c programming?

Its very easy.

There are different ways to convert string from lower case to upper case and vice versa.

We will discuss following ways to convert lowercase to uppercase in c.

  1. Using Loop- for, while and do while loops
  2. Using Predefined function toupper()
  3. Creating own function

Lets see them one by one.

1 Using Loops to convert lowercase character to uppercase character

Characters in C programming is represented by ASCII numbers.

So each character have its own ASCII number.

for a it is 97 for b it is 98 similar for z it is 122

Also for A it is 65 for B it is 66 and for Z it is 90.

We can check each character in character array if its values is between 97 to 122 then we can substract 32 from that character to get its Capital letter.

Using loops we are going to explain c program to convert lowercase to uppercase without using library function

Steps for program is as below

  1. Declare a character array char str[50];
  2. Accept character array from user fgets(str,sizeof(str),stdin);
  3. Loop character array until you get ‘\0’ (represents last or character array) for(i=0;str[i]!='\0';i++)
  4. Inside loop check if a character is between a to z if(str[i]>='a' && str[i]<='z')
  5. If yes then subtract 32 from that character str[i] -=32;
  6. You will get Uppercase string. printf("Upper case string is :%s",str);

A. Using For Loop to convert lowercase character to uppercase character

For loop is used to get each character form start to end one by one

Output

In above program we checked str[i] with a and z.

Instead of characters it can also use a and z ASCII values as below

B. Using While Loop to convert lowercase character to uppercase character

C. Using Do While Loop to convert lowercase character to uppercase character

2. Using toupper() function to convert lowercase character to uppercase character

C program to convert lowercase to uppercase using string function

Using toupper() function can easily convert lowercase character to upper case.

To use toupper() you have to include ctype.h header file

Syntax of toUpper() is

int toupper(int c);

This function takes one int argument and convert in to Upper case character and return its integer value.

Steps to convert lowercase character to uppercase is as below

  1. Declare a character array char str[50];
  2. Accept character array from user fgets(str,sizeof(str),stdin);
  3. Loop character array until you get ‘\0’ (represents last or character array) for(i=0;str[i]!='\0';i++)
  4. Inside loop use toupper() method to convert lower case character to upper case str[i]=toupper(str[i]);
  5. You will get Uppercase string. printf("Upper case string is :%s",str);

3. Creating a function to convert lowercase character to uppercase character

A. Passing each character to convert it to uppercase

Here one one character of character string is converted to Upper case using Function toUpper().

B Passing entire String to Covert to upper case

Here entire character string is passed to a function and converted to upper case.

We have discussed simple methods of conversion lower case to upper case in c. You can apply any one of the above as per your need.

Read More

  1. C++ program for student details
  2. Anonymous Object in C++
  3. Command Line Argument
  4. Local vs Global Object in C++
  5. Local and Nested Classes in C++
  6. Arithmetic operations using switch case
  7. Switch Case in C Programming
  8. Constructor in C++
  9. Know more about C++

Addition of two numbers in C Programming

Addition of two numbers in C Programming

Addition of two numbers in C Programming is basic and simple task.

We can create it very easily.

Writing a program is based on Algorithm.

An algorithm is step by step description of program.

So before writing program we must write algorithm to develop this program.

There are different ways to represent an algorithm

1 Flow chart
2. Pseudocode

Flow chart to add two numbers

Addition of two numbers in C

Pseudocode to add Two Numbers

  1. Start Program
  2. Input two numbers a and b
  3. sum=a+b
  4. Print sum
  5. End Program

Example Addition of two numbers in C

here %d is used to print integer value we can also use %i.

Result

Addition program in C with user Input

Fig: Data Type Promotion in addition

Here one variable is int type another is float type.

So int value is automatic promoted to float value

now both are float and its result is float.

This automatic promotion is known as widening.

Output

Addition program in C of Two long numbers

C Program to add two float numbers

C Program to add two double numbers

Addition of two numbers in C using command line arguments

gcc add.c

./a.out 6 4

Addition of two numbers in C using command line arguments
Fig: Addition of two numbers in C using command line arguments

Here function atoi() is used to convert string to integer value

Addition of two numbers in C using function

We discussed addition of two numbers in C Programming.

Break and Continue Statement in C with Example

Break and Continue Statement in C are looping control statements. Based on break and continue statement loop can be break or skipped

break and continue both are keywords.

break – Uses to break the loop also use with switch to break the switch case.

continue– skip the current execution of loop and transfer control to beginning of loop

For Loop in C programming
While Statement in C Programming
Do While Statement in C Programming

Break Statement in C

To terminate or end the current loop execution break statement is used.

Example of Break Statement in For Loop

Inside for loop we set some condition inside if statement if condition is true then break statement executes and transfer control to outside loop.

Output

Example of Break Statement in While Loop

Example of Break Statement in do While Loop

Example of Break Statement in Switch Statement

To terminate switch break statement break is used inside switch case statement

Switch Case in C

Example of Break Statement in Nested Loop

Here break keyword is used inside two loops to check whether two array element matches or not.

if element found then break is used to break inner loop.

Example of Break Statement In User Control Loop

In this program array name is two dimensional array that stores names.

We get input from user and store in name array.

if array is full or user type Bye then we call break statement to end the loop.

Finally we display all names.

This way an infinite loop can be controlled by user.

Continue Statement in C

Continue is used to skip the current loop execution and transfer control to beginning of loop.

Example of Continue Statement in C For Loop

loop will start from 1 and end when it is greater than or equals to 6.

inside loop a condition is checking that i==3 if true then continue is statement is get executed which transfers the control to beginning of loop.

Example of Continue Statement in While Loop

Example of Continue Statement in Do While Loop

What is the use of continue statement in C

Continue is used to skip the current loop iteration and transfer the control to beginning of loop.

What is the use of break statement in C

Break is used to break a inner most loop.

break vs continue in C

break is used to terminate the loop where as continue is used to skip current iteration of loop.

here when i=5 then loop will be terminated

when i=5 then loop will not continue current iteration. It will start with other iteration

Count Repeated Elements in an Array in C Programming

Count Repeated Elements in an Array is generally ask in interviews.

This problem can be solved in any language.

Here we will discuss algorithm to count repeated elements and implement it on C programming.

Count Repeated Elements in an Array in C Programming
Fig: Count Repeated Elements in an Array in C Programming

What is Repeated Elements

Repeated elements are elements that occurs more than one time in a list or in a sequence.

Consider following list

1 2 3 4 5 6 7 7 2 3 3 3

Here element 2 is repeating 2 times 3 is repeating 4 times and 7 is repeating 2 times.

This thing we have to implement in programming.

So our input is list or sequence on elements.

and output is repeated elements and repetition number.

So how we will turn our input to output?

Algorithm to Count Repeated Elements

Lets define our Algorithm that will get input and give use desired output.

  1. Create Array arr[] and repeated[][]
  2. Take input from user and store to arr
  3. For each element of arr check is it in repeated array or not
  4. If element found then increase element count
  5. Else add element to repeated array
  6. Print repeated array that has count 2 or more

Above is our simple step to develop our program

Program to Count Repeated Elements in C Programming

Based on above algorithm we develop this program.

Result

Hope you learn from this example

C program for arithmetic operations using switch case and do while loop

A switch case is used to select one among multiple options.

To perform arithmetic operations, we create case for each operator, for matching operator it will call appropriate case statement and execute the statements.

In our program we will use switch case statement and do while loop statement

You can check both details here

  1. Switch Case Statement
  2. Do While Loop Statement

C program for arithmetic operations using switch case

Lets see steps to write this C program

  1. Declare variables
  2. start loop
  3. get num1 ,operator, num2 value from user
  4. start switch case and pass operator in switch case
  5. write case for arithmetic operators and its associated statements
  6. define default in switch case
  7. Take input from user to check loop continuity
  8. if user enters yes the repeat step 2-7 else end the loop.

Output

Read More

  1. C++ program for student details
  2. Anonymous Object in C++
  3. Lowercase character to uppercase character
  4. Command Line Argument
  5. Local vs Global Object in C++
  6. Local and Nested Classes in C++
  7. Switch Case in C Programming
  8. Constructor in C++
  9. Know more about C++

Primitive Data Types in C Programming

1. What is data type in C

Data type specifies what type of data a variable can hold.

There are four category of data type in C language. 

They are as follows:

Types Data Types
Basic (Primitive) data types int, char, float, double
Enumeration data type Enum
Derived data type pointer, array, structure, union
Void data type void

2. Primitive Data Types In C Language

There are two types of qualifiers are used with primitive data types

  1. Size Qualifier – short, long
  2. Sign Qualifier- signed, unsigned

Size qualifier specifies the size and range of data type short qualifier is used to store less amount of data compared to long qualifier.

Sign qualifier specifies whether a data type can store only positive value or positive and negative both.

By default a data type is signed qualifier.

Size of primitive data type depends on computer system and word size.

So our data type may vary with others.

If you want to check data size of int, char, float double you can use sizeof operator.

Example: How to find size of data types in c language.

Above Program shows the size of int, size of short, size of long, size of char, size of float, size of double data types in C. Similar way you can find size of all data types in C.

Following are details of data types with format specifier in c, range of data types in c.

1 Integer Data Type

Integer data ( example 1,2,3,4,5,6, ….. etc. ) is stored in int ,short and long data type.

Type Storage size Value range Format Specifier
int 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 %d or %i
unsigned int 4 bytes 0 to 65,535 or 0 to 4,294,967,295 %u
short 2 bytes -32,768 to 32,767 %hd
unsigned short 2 bytes 0 to 65,535 %hu
long 4 bytes -2,147,483,648 to 2,147,483,647 %ld
unsigned long 4 bytes 0 to 4,294,967,295 %lu

Example: Addition of integer number.

Output

Note: Here %i is a format specifier which is used to print integer value.

2. Character Data Type

Character data ( example ‘a’ , ‘b’ , ‘f’, ….. etc. ) is stored in “char” data type.

Type Storage size Value range Format Specifier
unsigned char 1 byte 0 to 255 %c
signed char 1 byte -128 to 127 %c

Example: Declare and initialize character variable.

Output

3. Float and Double Data Types

Floating point value (example  11.23, 333.3330 etc. ) can be stored in data type float and double.

Small floating value can be stored in float and large floating stored in double. 

Type Storage size Value range Format Specifier Precision
float 4 byte 1.2E-38 to 3.4E+38 %f ,%e or %E for scientific notation 6 decimal places
double 8 byte 2.3E-308 to 1.7E+308 %lf 15 decimal places
long double 10 byte 3.4E-4932 to 1.1E+4932 %Lf 19 decimal places

Example: Declare and initialize float & double variable.

Output:

Array in C – Declare Initialize Access and Operations

Here We learn Array in C how to Declare Initialize and access array and operations on array.

Array is a data structure that can store elements of the same type.

Array is a collection of similar type elements

It stores the elements in a contiguous memory locations.

1 Why Array Required?

Array is used to store similar types of values under same name

 We want to declare 100 integer variable

Instead of declaring 100 individual variables, such as

int number0, number1,number2,number3, ... …., number99;

you can declare one integer array variable such as

int number[100];

here number[100] is an integer array of size 100, means this array can store 100 integer value.

Array indexing starts from index 0 to n-1. Means first integer number store in array number[0], second integer number store in array number[1], third integer number store in array number[2], and so on. Here 100th  number store in array numbers[99].

2 How to Declare an Array in C?

Syntax of declare an array in C is as below

DataType  arrayName [ arraySize ];

This is called a single-dimensional array.

DataType : any valid C data type like int, char, float, double, long
array Name: Any valid c identifier
arraySize: an integer constant greater than zero.

For Instance

  1. int age[5];
  2. long length[8];
  3. float price[7];
  4. double value[5];
  5. char ch[5];

Above all are example of array.

  1. age is a integer array of size 5.
  2. length is a long array of size 8
  3. price is a float array of size 7
  4. value is double array of size 5
  5. ch is a character array of size 5

Size tells us that a array can store maximum element up to size.

Size must be a integer constant. based on size array occupies memory location for elements.

There are multi dimensional array.

3 Array Initialization

Giving initial value to array is know as array initialization.

A Initializing each member

We can initialize each element one by one as below.

int age[5]; //declaration

age[0]=21;
age[1]=24;
age[2]=25;
age[3]=27;
age[4]=26;

Array uses zero based indexing so first element is stored in age[0] and last element will store in size-1 position here age[4];

B Declare and initialize

general syntax is

DataType arrayName[size]={value1,value2,.............,valueN};

example

int age[5]={21,24,25,27,26};

value 21 will store in age[0] and value 24 will store in age[1] and similar for other values.

While initializing array giving size is optional;

like

float price[]={22.2,44.2,67.33,55.66,43.2};

Some times we want to initialize only few elements of array in that case we have to provide size of array and initialize only few values other values are initialized by zero.

double value[20]={10.0,21.0};

Here from value[3] to value[19] will be initialize by 0.0

4 Access Array Elements

Array uses zero based indexing to access elements.

to access element of an array, we can use syntax

arrayName[index];

int age[5]={21,24,25,27,26};

age[0], age[1],age[2],age[3],age[4], will access 21,24,25,27 and 26 respectively.

Lets see few examples to store values in array and access array values.

Example: write a program to create integer array and store 5 integer number and print.  

Description : In the above program,   int  num[5] array can store 5 element in a contiguous memory locations from index 0 to 4.         

Example: write a program to take 5 number from user and store integer number in integer array and print.  

OUTPUT

Example: Print the sum of all elements of an array

Output

5 Operations on Array

On array we can perform following basic operations

  1. Traverse– accessing all element of array
  2. Insertion– adding a new element at specified position
  3. Update – changing the existing value at specified position
  4. Deletion– deleting an element from specified position
  5. Search– search a element in array

A Traverse an array

Traversing is accessing the element of array.

Here we will access each element and print it

Output

B. Inserting an element in Array

add a new element in array can be done at various position

  • Insertion at the beginning of array
  • add at the give index position
  • Insert at last

Here we will insert a element based on index position.

On inserting a new element at specified position next element will move one space forward in array.

Output

C Update an element in Array

To update an array element we have to select element based on index value and provide new value to replace the existing one.

Simple program to update array value based on index is as below

Output

D Delete an element in Array

To delete an array element First select index value of element.

After deleting array element other elements are shifted forward to fill the gap of deleted element.

Output

E Search an element in Array

Search operation is used to find an element in array.

If element fond return index and element value.

If not it will return -1.

Output

F Sorting elements in Array

Arranging array elements in ascending or in descending order is known as sorting of array.

Following program is used to sort array elements in ascending order.

Output

Command Line Argument in C Programming

In a C programming often a situation may come where we want to pass the information into a program during the execution of program.

These values are called Command Line Arguments(CLA).

“main()” function handles the command line argument.

Syntax:

int main( int argc, char *argv[] )
{

}

Here, argc refers to the number of arguments passed  and argv[] is a pointer array which points to each argument .

Note:

argc – argument count in command line argv[0]– holds the name of the program
argv[1]– holds the first command line argument,
argv[2]– holds the second command line argument and and so on.
If no argument is supplied, argc will be 1

          

Program1: Write a program in a C to take one value from user during execution as a command line argument.

Note: Suppose above program name is “CLA”.

Run this program as follows in Linux:

./CLA hello

Here hello is a command line argument.

Run this program as follows in Windows from command line:

program.exe  hello

 Here hello is a command line argument.

Output
No of command line arguments are 2
Program name is: CLA
Command line argument is: hello

Program2 : A C program to add two numbers using command line angument.

to convert string to integer atoi() method is used. Similar to convert string to float atof() is used

Compiling and running above program

gcc add.c ./a.out 5 6

Output
sum is 11
Program3: Write a program in a C to take 4 names input from command line argument and print it.Note: Suppose above program name is “names.c”.

gcc names.c ./names RAM MOHAN SOHAN RAMESH

Output
Program name : names
1st arg : RAM
2nd arg : MOHAN
3rd arg : SOHAN
4th arg : RAMESH

Program4: Find the radius of circle take area input from command line argument

gcc circle.c ./circle 5

Output
Area of circle = 78.50

Read More

  1. C++ program for student details
  2. Anonymous Object in C++
  3. Lowercase character to uppercase character
  4. Local vs Global Object in C++
  5. Local and Nested Classes in C++
  6. Arithmetic operations using switch case
  7. Switch Case in C Programming
  8. Constructor in C++
  9. Know more about C++

File handling programs in C Programming

Example : Write a C program to copy the contents of one file to another file.

OUTPUT

Example : Write a C program to appends the content of file at the end of another file.

Or

 Example : Write a C program to merge two files into third file.

Example: Write a C program to count spaces ,vowels, consonants, digits in a file .

Writing to a File in C Programming

To write on a file there are following  function available:

  1.  Function fputc() : function fputc() write individual characters at a time to the file. The syntax of this function is –

Syntax :

               int  fputc( int c,  FILE *fp );

Function fputc()  returns the written character written on success and  if there is an error it will return EOF (Emd of File).

  • Function fputs() :  function fputs() writes the string s’ to the output stream referenced by fp. The syntax of this function is –

Syntax :

               int fputs( const char *s, FILE *fp );

Function fputs()  returns the non-negative value  on success and  if there is an error it will return EOF (Emd of File).

  • Function fprintf(): Function fprintf()write a string into a file. The syntax of this function is –

Syntax :

int  fprintf(FILE  *fp,  const  char *format, …)

Example: Write a C program to write a single character into file using fputc() function in file handling.

OUTPUT

a
BOOK1.txt

 Example: Write a C program to write a string into a file using fputs() and fprintf() function in file handling.

OUTPUT

Hello  hii  how are you
I am aditya

Book1.txt

Description: When  we run the above program , it creates a new file Book1.txt in /c directory and writes two lines using fputs() and fprintf() functions.

Example : Write a C program to open a file, write in it and close the file.

  OUTPUT

i am aditya from it department

Book1.txt