PDF Resize
PDF Resize
PROGRAMMING IN C
Introduction of C:
8C9 seems a strange name for a programming language. But this strange sounding
language is one of the most popular computer languages today because it is a structured,
high-level, machine independent language. It allows software developers to develop
programs without worrying about the hardware platforms where they will be
implemented.
8C9 language was developed by Dennis Ritchie at Bell laboratories in 1972.
In 1988, the American National Standards Institute (ANSI) had formalized the C
language.
C was invented to write UNIX operating system.
C is a successor of 'Basic Combined Programming Language' (BCPL) called B language.
Linux OS, PHP, and MySQL are written in C.
C has been written in assembly language.
C has been popular for the following reasons:
A character denotes any alphabets, digit or special symbol used to represent information.
Keywords:
Keywords are the words whose meaning has already been explained to the C compiler. All
keywords must be written in lower case. There are 32 keywords in C. Keywords cannot be
used as variable name.
do if static while
Token:
The smallest individual unit in a program is called C token. C token has six tokens.
i) int: - int is the keyword for integer. It contains the whole numbers between -32,768 to
32,767. It requires 2 bytes memory and its type specifier is %d.
ii) float: - float is the keyword for floating numbers i.e. fractional numbers. It contains
number 3.4e – 38 to 3.4e+38. It requires 4 bytes memory and its type specifier is %f.
Example: - float pi=3.14;
iii) char: - char is the keyword for character. It represents the single alphabet. It requires 1-
byte memory and its type specifier is %c. Example: - char choice;
Variable: -
A variable is a data name that may be used to store a data value. A variable may take
different values at different times during program execution. Variable names may consist of
letters, digits and the underscore (_) characters. A variable name can be chosen by the
programmer in a meaningful way so as to reflect its functions or nature in the program.
3. Upper case and lower case are significant that is, the variable area is not the same as Area
or AREA.
Types of variable: -
Numeric Variable: - The variables which stores the numeric data only is called numeric
variable. It can be whole number or decimal number. Example: 10, 11, 3.14 etc.
String Variables: - The variable which stores the character data only is called String
variable. It can be a single character or string. Examples: 8a9, <apple= etc.
Constant: -
A constant is a value that does not change during the program execution. There are several
types of constants. They are:
a) Integer constant: - An integer constant refers a sequence of digits. Examples: 123, 23, 0
etc.
b) Real (Floating constant): - Floating point constant contains a decimal point. Examples:
3.14, 6.12, etc
c) Character: - A character constants are enclosed within single quote. Examples: 8a9, 8b9
etc.
d) String constant: - A string constant is a sequence of characters enclosed in double quote.
Examples: <Hi this is PLK sir=, <2015= etc.
OPERATOR
An operator is a symbol that instructs C to perform some operation on one or more operands.
The data on which operator are performed is called Operand. For example: p=3+4; P, 3 and 4
are operands whereas = and + are operator and p=7 is the result.
1. Arithmetic Operator:
Operators which are used in mathematical expression are called arithmetical operators. The
various arithmetic operators are:
These operators are generally used in conditional expression. The three logical operators in C
are as follows:
3. Assignment Operator
The assignment operator calculates the expression on the right side and gives the values of
left side variables.
Example:- a=2+3=5
V op = exp;
Here, at first the arithmetic operation is performed than only assignment operation is
performed.
4. Relational Operator
It is used to compare the values between operands and gives a result whether it is true or
false. It is also called comparison operator due to its comparing characteristics. There are 6
relational operators are:
= = Equal to
!= Not equal to
5. Ternary Operator (? :)
C has only ternary operator of this operators result in terse (shorten) and compact mode.
Syntax:
Example:
int x,a,b;
a=6;
b=10;
x= (a<b)? a:b;
printf("%d",x);
The increment and decrement operator are very useful in C language. The syntax of operator
is:
7. Comma Operator
The comma operator can be used to link the related expressions together. A comma-linked
list of expressions is evaluated left to right and the value of right-most expression is the value
of the combined expression. For examples: The statement
Header file:
A header file is a file with extension .h which contains C function declarations and
macro definitions to be shared between several source files.
#include<stdio.h>
It is the standard input/output header file. It contains the function definition of input
output functions such as scanf(), printf(), etc.
#include<conio.h>
It is a header file use in console input output function such as clrscr() , getch(), etc.
#include<math.h>
This header file is used for mathematical function such as pow(), sqrt(), sin(), etc.
#include<string.h>
This header file is used for string processing such as strlen(), strcpy, etc.
The statement which display one statement when the condition is true, otherwise display
another statement is known as decision-making statement. Since these statement <control= the
flow of execution, they are also known as control statements.
a) Simple if statement
b) if –else statement
d) else---if ladder
a) Simple if Statement
Syntax:
{
Statement-block;
}
Statement-x;
Consider the following segment of a program that is written for processing of marks obtained
in an entrance examination.
………………….
………………….
if (category= = sports)
printf(< %f =,marks);
………………….
………………….
The program tests the type of category of the student. If the student belongs to the SPORTS
category, then additional bonus_marks are added to his marks before they are printed. For
others, bonus_marks are not added.
b) if---else statement
if (test expression)
else
False-block statement(s);
Statement-x;
………………….
………………….
if(code= = 1)
boy= boy+1;
if(code= = 2)
girl=girl+1;
………………….
………………….
The first test determines whether or not the student is a boy. If yes, the number of boys is
increased by 1 and the program continues to the second test. The second test again determines
whether the student is a girl. This is unnecessary. Once a student is identified as a boy, there
is no need to test again for a girl, not both. The above program segment can be modified
using the else clause as follows.
………………….
………………….
if(code = = 1)
boy=boy+1;
else
girl=girl+1;
………………….
if(test condition-1)
if(test condition-2)
Statement-1;
else
Statement-2;
else
Statement-3;
Statement-x;
If the condition-1 is false, the statement-3 will be executed; otherwise it continues to perform
the second test. If the condition-2 is true, the statement -1 will be executed; otherwise the
statement-2 will be executed and then the control is transformed to the statement –x.
A commercial bank has introduced an incentive policy of giving bonus to all its deposit
holders. The policy is as follows: A bonus of 2% of the balance held on 31 st December is
given to everyone, irrespective of their balance, and 5% is given to female account holders if
their balance is more than Rs. 5000. This logic can be coded as follows:
…….
if(sex is female)
if(balance >5000)
bonus=0.05*balance;
else
bonus=0.02*balance;
else
bonus=0.02*balance;
balance=balance+bonus;
……………
d) The else---if ladder
This is another way of putting ifs together when multipath decisions are involved. A
multipath decision is a chain of ifs in which the statement associated with each else is an if.
if (condition 1)
statement-1;
else if (condition 2)
statement-2;
else if (condition 3)
statement-3;
else if (condition n)
statement-n;
else
default - statement;
statement –x;
This construct is known as the else - if ladder. The conditions are evaluated from the top (of
the ladder) to downwards. As soon as a true condition is found, the statement associated with
it is executed and the control is transferred to the statement – x (skipping the rest of ladder).
When all the <n= conditions become false, then the final else containing the default –
statement will be executed.
Let us consider an example of grading the students in an academic instituting. The grading is
done according to the following rules:
80 to 100 Distinction
60 to 79 First Division
50 to 59 Second Division
40 to 49 Third Division
0 to 39 Fail
if(marks>79)
grade==Distinction=;
else if(marks>59)
grade==First Division=;
else if(marks>49)
grade==Second Division=;
else if(marks>39)
grade==Third Division=;
else
grade==Fail=;
printf(<%s\n=,grade);
The switch statement tests the value of a given variable (or expression) against a list of case
values and when a match is found, a block of statements associated with that case is
executed. The general form of the switch statement is as shown below:
switch(expression)
case value-1:
block-1;break;
case value-2:
block-2;
break;
…………………….
…………………….
default:
default – block;
break;
statement – x;
When the switch is executed, the value of the expression is successfully compared against the
values value-1,value-2,..
If a case is found whose value matches with the value of the expression, then the block of
statements that follow the case are executed.
The break statement at the end of each block signals the end of a particular case and causes
an exit from the switch statement, transferring the control to the statement-x following with
switch.
The default is an optional case. When present, it will be executed if the value of the
expression does not match with any of the case values. If not present, no action takes place if
all matches fail and the control goes to the statement-x.
Let us consider an example, which displays the name of 7 days according to input numbers.
This can be shown as follows:
switch(ch)
case 1:
printf(<Sunday=);
break;
case 2:
printf(<Monday=);
break;
--------------
--------------
case 7:
printf(<Saturday=);
defaults:
Looping:
The process of executing the same statement repeatedly until a condition
is satisfied is called looping.
1. For loop
2. While loop
3. Do-while loop
1. For Loop
The for loop is applied in the situation when you exactly know how many
times you want to execute the statements.
--------------------------
--------------------------
Example:
#include<stdio.h>
#include<conio.h>
main( )
int i;
for(i=1;i<=10;i++)
getch ( );
2. While Loop
Initializations;
while(condition)
Statements
……………………….
Increment/decrement
#include<stdio.h>
#include<conio.h>
main( )
int i=1;
while(i<=10)
{
printf("I am a student of calss 12\n");
i++;
getch( );
3. do-while loop
The working of do-while loop is identical to that the while loop, except
that in the do-while loop, a condition is checked at the end of the loops
after each iteration. It means that the do-while loop definitely executes at
least once even if the condition is false. Unlike the while loop, a
semicolon has to be placed after while expression in the do-while loop;
otherwise, the loop will not execute.
initialization;
do
Statements;
Increment/decrement
}
while condition ( );
#include<stdio.h>
#include<conio.h>
main()
int i=1;
do
{
i++;
}while(i<=5);
getch();
Nested loop
6. Example: 6. Example:
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
main( ) main( )
{ {
while(i<=10) do
{ {
printf("I am a printf(" I am a
student of calss 12\n"); student of calss 12\n");
i++; i++;
} }while(i<=5);
getch( ); getch( );
} }
Infinite loop
The loops that do not end are called infinite loop. Generally,
infinite loops are used in server code where the servers have to
run without any interruption.
Example:
for ( ; ;)
1. Break statement:
The break statement is used to exit from a while, for, do-while or switch
structures. It can only be used inside the body of a for, while, do-while,
or switch statement.
2. Continue Statement:
The continuous causes the loop to be conditioned with the next iteration
after skipping any statements in between. The continue statements tells
the compiler <skip the following statements and continue with the next
iteration=.
5. Example: 5. example:
main( ) main( )
{ {
int i; int i;
{ {if(i= =2)
break; printf("%d\t",i);
printf("%d",i); }
} getch( );
getch ( ); }
} Output:
Output: 1 1 3 4 5 6 7 8 9 10
Homework Questions
C-1 (Introduction of C)
1.1 Define data type. Explain different types of data types used in C
programming with examples.
1.3 What is variable? Write its types. Define the terms identifier and
keywords with example.
THE END
PROGRAMMING PART-1
#include<stdio.h>
#include<conio.h>
main( )
{
int n;
printf("Enter any number=");
scanf("%d",&n);
if(n%2==0)
printf("The entered number is even.");
else
printf("The entered number is odd.");
getch( );
}
2. WAP to accept any 3 numbers and print the largest number among them?
#include<stdio.h>
#include<conio.h>
main( )
{
int a,b,c;
printf("Enter any three numbers=");
scanf("%d%d%d",&a,&b,&c);
if(a>b&&a>c)
printf("The largest integer=%d",a);
else if(b>a&&b>c)
printf("The largest integer=%d",b);
else
printf("The largest integer=%d",c);
getch( );
}
3. WAP that checks whether the number entered by user is exactly divisible by 5
but not by 11?
#include<stdio.h>
#include<conio.h>
main( )
{
int n;
printf("Enter a number =");
scanf("%d",&n);
if(n%5==0&&n%11!=0)
printf("The number is %d.",n);
else
printf("Exit.");
getch( );
}
4. WAP that enter SP and CP and determine whether there is profit of loss?
#include<stdio.h>
#include<conio.h>
main( )
{
int sp,cp,p,l;
printf("Enter the sp and cp=");
scanf("%d%d",&sp,&cp);
if(sp>cp)
{
p=sp-cp;
printf("Profit=%d",p);
}
else
{
l=cp-sp;
printf("Loss=%d",l);
}
getch( );
}
#include<conio.h>
main( )
{
int n,i,sum=0;
printf("Enter a number=");
scanf("%d",&n);
for(i=1;i<=n;i++)
if(i%2==0)
sum=sum+i;
getch( );
10. WAP to check whether the input number is prime number or not?
#include<stdio.h>
#include<conio.h>
main( )
{
int i, n;
printf("\nEnter any number : ");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
{
printf("\nThe number %d is not prime",n);
break;
}
}
if(i==n)
printf("\nThe number %d is prime",n);
getch( );
}
for(i=1;i<=10;i++)
{
c=a+b;
a=b;
b=c;
printf("%d\t",c);
}
getch();
}
13. WAP to display 1 for Sunday, 2 for Monday etc.
#include<stdio.h>
#include<conio.h>
main ( )
{
int choice;
printf("Enter the numbers of the days=");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break;
case 4:
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
case 7:
printf("Saturday");
break;
default:
printf("Wrong Choice");
}
getch( );
}
#include<stdio.h>
#include<conio.h>
main()
{
int num;
printf("enter a number=");
scanf("%d",&num);
printf("The number in reverse order=");
printf("%d",num%10);
num=num/10;
printf("%d",num%10);
num=num/10;
printf("%d",num%10);
num=num/10;
printf("%d",num%10);
getch( );
}
15. WAP to display 1
12
123
1234
12345 ?
#include<stdio.h>
#include<conio.h>
main()
int i,j;
for(i=1;i<=5;i++)
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
getch( );
22
333
4444
55555 ?
#include<stdio.h>
#include<conio.h>
main ( )
int i,j;
for(i=1;i<=5;i++)
for(j=1;j<=i;j++)
printf("%d",i);
printf("\n");
getch( );
**
***
****
***** ?
#include<stdio.h>
#include<conio.h>
main()
int i,j;
for(i=1;i<=5;i++)
for(j=1;j<=i;j++)
printf("*");
printf("\n");
getch( );
321
21
1 ?
#include<stdio.h>
#include<conio.h>
main ( )
int i,j;
for(i=4;i>0;i--)
for(j=i;j>0;j--)
printf("%d",j);
printf("\n");
printf(" ");
getch( );
}
19. WAP to display 12345
1234
123
12
1 ?
#include<stdio.h>
#include<conio.h>
main()
int i,j;
for(i=5;i>=1;i--)
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
getch();
4444
333
22
1 ?
#include<stdio.h>
#include<conio.h>
main( )
int i,j;
for(i=5;i>=1;i--)
for(j=1;j<=i;j++)
printf("%d",i);
printf("\n");
}
getch( );
****
***
**
* ?
#include<stdio.h>
#include<conio.h>
main( )
int i,j;
for(i=5;i>=1;i--)
for(j=1;j<=i;j++)
printf("*");
}
printf("\n");
getch( ); }
22. WAP to enter a string and check whether the entered string is palindrome or not?
[A string is said to be palindrome if it remains same if we reverse it. Eg:-
ADA,LIRIL,MALAYALAM,MADAM]
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
char str[30],temp[30];
printf("Enter a string\n");
scanf("%s",str);
strcpy(temp,str);
strrev(temp);
if(strcmp(temp,str)==0)
printf("String is palindrome");
else
23. WAP to display the Armstrong number to =n= numbers. [An Armstrong number is that
whose sum of cube of digits is equal to number itself. Example:-371=33+73+13]
#include<stdio.h>
#include<conio.h>
main()
int a,j,arm,sum,k;
scanf("%d",&arm);
for(a=1;a<=arm;a++)
sum=0;
j=a;
while(j!=0)
{
k=j%10;
sum=sum+k*k*k;
j/=10;
if(a==sum)
printf("\n%d is Armstrong",sum);
getch();
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
int choice,num,i;
printf("\n\n1.Factorial number\n");
printf("2. prime\n");
printf("3.Odd or even\n");
printf("4.Multiplication table\n");
printf("5.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
scanf("%d",&num);
for(i=num;i>0;--i)
m=m*i;
printf("Factorial of %d =%ld",num,m);
break;
scanf("%d",&num); for(i=2;i<num;i++)
{ if(num%i==0)
break;
if(i==num)
case 3:
printf("Enter a number=");
scanf("%d",&num);
if(num%2==0)
break;
case 4:
printf("Enter number=");
scanf("%d",&num);
for(i=1;i<=10;i++)
{ int mult=num*i;
printf("\t %dX%d=%d\n",num,i,mult);
} break;
case 5:
exit(0);
default:
getch();
25. WAP to accept two numbers and performs Addition, subtraction, multiplication and
division?
#include<stdio.h>
#include<conio.h>
main()
{ char choice;
int n1,n2,result;
scanf("%c",&choice);
printf("Enter any two numbers\n");
scanf("%d%d",&n1,&n2);
switch(choice)
case'+':
result=n1+n2;
printf("sum=%d",result);
break;
case'-':
result=n1-n2;
printf("subtraction=%d",result);
break;
case'*':
result=n1*n2;
printf("Multiplication=%d",result);
break;
case'/':
result=n1/n2;
printf("Division=%d",result);
break;
case'%':
result=n1%n2;
printf("Remainder=%d",result);
break;
default:
getch();
#include<stdio.h>
#include<conio.h>
main()
{ char name[30];
int clas,eng,nep,phy,chem,cmp,total;
float per;
scanf("%d",&clas);
scanf("%d%d%d%d%d",&eng,&nep,&phy,&chem,&cmp);
total=eng+nep+phy+chem+cmp;
per=total/5;
printf("NAME=%s\n",name);
printf("CLASS=%d\n",clas);
printf("TOTAL=%d\n",total);
printf("Percentage=%0.2f\n",per);
if(eng>=35&&nep>=35&&phy>=35&&chem>=35&&cmp>=35)
printf("Result=Passed\n");
else
printf("Result=Failed\n");
if(per>=75)
printf("Division=Distinction");
else if(per>=60&&per<75)
printf("Division=First");
else if(per>=45&&per<60)
printf("Division=Second");
else if(per>=35&&per<45)
printf("Division=Thid");
else printf("Division=Fail");
getch();
} THE END
ARRAY
Definition: A composite variable capable of holding multiple data of same data type under a
single variable name is called array.
Declaration of Array:
Types of Array:
1) In 1-D array only one size is used. 1) In 2-D array two sizes are used.
3) Declaration: 3) Declaration:
5) In 1-D array size denotes either 5) In 2-D array size 1 denotes row
row or column. and size2 denotes column.
A1. WAP that inputs the marks of 5 students and display on the screen?
#include<stdio.h>
#include<conio.h>
main( )
int marks[5],i;
for(i=0;i<5;i++)
printf("Enter %d marks=",i+1);
scanf("%d",&marks[i]);
for(i=0;i<5;i++)
getch( );
}
OUTPUT:
A2. WAP to store 10 numbers and print the largest among them?
[HSEB-2064]
#include<stdio.h>
#include<conio.h>
main( )
int n[10],i,largest;
for(i=0;i<10;i++)
{
scanf("%d",&n[i]);
largest=n[0];
for(i=0;i<10;i++)
if(n[i]>largest)
largest=n[i];
getch( );
}
A3. WAP to find the largest number among <nth= numbers?
#include<stdio.h>
#include<conio.h>
main()
int i,n,num[100],max;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&num[i]);
}
max=num[0];
for(i=1;i<n;i++)
if(num[i]>max)
max=num[i];
getch();
[HSEB-2065,2067] 10 marks
#include<stdio.h>
#include<conio.h>
main( )
int i,j,n,num[100],temp;
for(i=0;i<n;i++)
scanf("%d",&num[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(num[i]>num[j])
temp=num[i];
num[i]=num[j];
num[j]=temp;
printf("%d \t",num[i]);
getch( ); }
A5. WAP to sort an array of <n= elements in descending order? [HSEB 2063,2068]
10 marks
#include<stdio.h>
#include<conio.h>
main( )
int i,j,n,num[100],temp;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&num[i]);
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(num[i]<num[j])
temp=num[i];
num[i]=num[j];
num[j]=temp;
for(i=0;i<n;i++)
printf("%d \t",num[i]);
getch( ); }
A6. WAP to input <n= numbers and find out the greatest and smallest number?
[HSEB-2062]
#include<stdio.h>
#include<conio.h>
main()
int n,i,max,a[100],min;
scanf("%d",&n);
for(i=0;i<n;i++)
printf("Enter %d number=",i+1);
scanf("%d",&a[i]);
max=a[0];
min=a[0];
for(i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
min=a[i];
else
printf("Greatest number=%d\n",max);
printf("Smallest number=%d",min);
getch();
A7. WAP to input names of <n= numbers of students and sort them in alphabetical
order? [HSEB-2062,2068] 10 marks
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char name[100][100],temp[100];
int i,j,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",name[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(strcmp(name[i],name[j])>0)
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
printf("Sorted Alphabetically\n");
for(i=0;i<n;i++)
printf("%s\n",name[i]);
getch( );
A8. WAP which reads salary of 25 employees and count the number of employees who
are getting salary between 30,000 to 40,000?
#include<stdio.h>
#include<conio.h>
main()
{
int salary[25],i,c=0;
for(i=0;i<25;i++)
scanf("%d",&salary[i]);
for(i=0;i<25;i++)
c=c+1;;
printf("Total number of employee who gare getting salary in the range of 30,000 to
40,000 are=%d",c);
getch();
}
A9. WAP to add two matrices by supplying elements of matrices by the user?
#include<stdio.h>
#include<conio.h>
main()
int m1[3][4],m2[3][4],m3[3][4],r,c;
for(r=0;r<3;r++)
for(c=0;c<4;c++)
scanf("%d",&m1[r][c]);
for(c=0;c<4;c++)
scanf("%d",&m2[r][c]);
for(r=0;r<3;r++)
for(c=0;c<4;c++)
m3[r][c]=m1[r][c]+m2[r][c];
printf("\n");
for(r=0;r<3;r++)
for(c=0;c<4;c++)
{
printf("%d\t",m3[r][c]);
printf("\n");
getch();
#include<stdio.h>
#include<conio.h>
main()
int i,j,temp;
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
temp=a[i][j];
a[i][j]=a[j][i];
a[j][i]=temp;
printf("Transpose matrixes\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf("%d\t",a[j][i]);
printf("\n");
getch();
Homework Questions
C-2 (Array)
2.1 What is an array? Differentiate between 1-D and 2-D array.
2.3 WAP to store 10 numbers and print the largest among them?
2.6 WAP to input <n= numbers and find out the greatest and smallest
number?
2.7 WAP to input names of <n= numbers of students and sort them in
alphabetical numbers?
2.8 WAP which reads salary of 25 employees and count the number of
employees who are getting salary between 30,000 to 40,000?
THE END
STRING
Definition: The collections of the characters are called string. It is always enclosed in double
quotation marks. It contains characters, symbols, numbers etc. Example: <Kathmandu=,
<12345= etc.
The c library supports many string handling functions that can be used to carry out many of
the string manipulation. The header file <string.h= is used for string manipulation functions.
1. Strlen(variable name): - This string function is used to find out the exact length
of the string. The return of the strlen function is integer value.
Syntax:
n= strlen(str);
where str is the string and n is the length of string , returned by strlen function.
Example:
#include<stdio.h>
#include<conio.h> Output:
#include<string.h>
main() Enter a string PLKSIR
{
int l; LENGTH=6
char str[100];
printf("Enter a string");
scanf("%s",str);
l=strlen(str);
printf("LENGTH=%d",l);
getch();
}
strcat(str1,str2);
Example:
#include<stdio.h>
#include<conio.h> Output:
#include<string.h>
main() Enter string1,string2
{
char a[100],b[100]; PLK
printf("Enter string1,string 2");
scanf("%s%s",a,b);
strcat(a,b); SIR
printf("After concatenating two string
=%s",a); After concatenating two string
getch(); =PLKSIR
}
strcmp(str1,str2);
Example:
#include<stdio.h>
Enter string1,string 2PLK
#include<conio.h> PLK
Both strings are same.
#include<string.h> //
Enter string1,string 2 PLK
main() SIR
Both strings are not same.
{
char a[100],b[100];
scanf("%s%s",a,b);
if(strcmp(a,b)==0)
else
printf("Both strings are not same.");
getch();
strrev(str);
Example:
#include<stdio.h> Output:
main()
char str[100];
printf("Enter a string");
scanf("%s",str);
printf("REVERSE ORDER=%s",strrev(str));
getch();
}
5. Strupr(string):- This is used to change case of characters.
Syntax:
Struprr(str);
Example:
#include<stdio.h>
#include<conio.h> Output:
#include<string.h>
main() Enter a string plk
{
char str[100]; UPPER CASE=PLK
printf("Enter a string");
scanf("%s",str);
printf("UPPER CASE=%s",strupr(str));
getch();
}
Strlwr(str);
Example:
#include<stdio.h> Output:
#include<conio.h>
#include<string.h> Enter a string PLKcmP
main()
{ lower case=plkcmp
char str[100];
printf("Enter a string");
scanf("%s",str);
printf("lower case=%s",strlwr(str));
getch();
}
Syntax:
strcpy(str1,str2);
Where str1,str2 are two strings and content of string2 is copied on a string str1.
Example:
#include<stdio.h> Output:
main()
char str1[100],str2[100];
scanf("%s",str1);
strcpy(str2,str1);
getch();
Homework Questions
examples.
with example.
THE END