C Language Tutorials by Ghulam Murtaza Dahar - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Work Of Function

strlen()

Calculates the length of string

strcpy()

Copies a string to another string

strcat()

Concatenates(joins) two strings

strcmp()

Compares two string

strlwr()

Converts string to lowercase

strupr()

Converts string to uppercase

Strings handling functions are defined under "string.h" header file, i.e, you have to include the code below to run string handling functions.

#include <string.h>

gets() and puts()

Functions gets() and puts() are two string functions to take string input from user and display string respectively as mentioned in previous chapter.

#include<stdio.h>

int main(){

char name[30];

printf("Enter name: ");

gets(name); //Function to read string from user.

printf("Name: ");

puts(name); //Function to display string.

return 0;

}

Though, gets() and puts() function handle string, both these functions are defined in "stdio.h" header file.

String Examples in C Programming

This page contains examples and source code on strings in C programming. To understand all the example on this page, you should have basic knowledge of string, passing string to function and few commonly used standard libraryfunctions to manipulate strings.

Examples of Strings in C Programming

C Programming Examples And Source Code

C Program to Find the Frequency of Characters in a String

C Program to Find the Number of Vowels, Consonants, Digits and White space

in a String

C Program to Reverse a String by Passing it to Function

C Program to Find the Length of a String

C program to Concatenate Two Strings

C Program to Copy a String

C Program to Remove all Characters in a String except alphabet

C Program to Sort Elements in Lexicographical Order (Dictionary Order)

C Program to Change Decimal to Hexadecimal Number and Vice Versa

C Program to Convert Hexadecimal to Octal and Vice Versa

C Program to Convert Binary Number to Hexadecimal Vice Versa

C Programming Structure

Structure is the collection of variables of different types under a single name for better handling. For example: You want to store the information about person about his/her name, citizenship number and salary. You can create these information separately but, better approach will be collection of these information under single name because all these information are related to person.

Structure Definition in C

Keyword struct is used for creating a structure.

Syntax of structure

struct structure_name

{

data_type member1;

data_type member2;

.

.

data_type memeber;

};

We can create the structure for a person as mentioned above as:

struct person

{

char name[50];

int cit_no;

float salary;

};

This declaration above creates the derived data type struct person.

Structure variable declaration

When a structure is defined, it creates a user-defined type but, no storage is allocated. For the above structure of person, variable can be declared as:

struct person

{

char name[50];

int cit_no;

float salary;

};

Inside main function:

struct person p1, p2, p[20];

Another way of creating sturcture variable is:

struct person

{

char name[50];

int cit_no;

float salary;

}p1 ,p2 ,p[20];

In both cases, 2 variables p1, p2 and array p having 20 elements of type struct person are created.

Accessing members of a structure

There are two types of operators used for accessing members of a structure.

1. Member operator(.)

2. Structure pointer operator(->) (will be discussed in structure and pointers chapter)

Any member of a structure can be accessed as: structure_variable_name.member_name

Suppose, we want to access salary for variable p2. Then, it can be accessed as:

p2.salary

Example of structure

Write a C program to add two distances entered by user. Measurement of distance should be in inch and feet.(Note: 12

inches = 1 foot)

#include <stdio.h>

struct Distance{

int feet;

float inch;

}d1,d2,sum;

int main(){

printf("1st distance\n");

printf("Enter feet: ");

scanf("%d",&d1.feet); /* input of feet for structure variable d1 */

printf("Enter inch: ");

scanf("%f",&d1.inch); /* input of inch for structure variable d1 */

printf("2nd distance\n");

printf("Enter feet: ");

scanf("%d",&d2.feet); /* input of feet for structure variable d2 */

printf("Enter inch: ");

scanf("%f",&d2.inch); /* input of inch for structure variable d2 */

sum.feet=d1.feet+d2.feet;

sum.inch=d1.inch+d2.inch;

if (sum.inch>12){ //If inch is greater than 12, changing it to feet.

++sum.feet;

sum.inch=sum.inch-12;

}

printf("Sum of distances=%d\'-%.1f\"",sum.feet,sum.inch);

/* printing sum of distance d1 and d2 */

return 0;

}