PPSC - Unit - 3
PPSC - Unit - 3
PPSC - Unit - 3
The control character „\0‟ which represents a null character is placed automatically at the
end of any string used.
int n;
This will return the length of the string 9 which is assigned to an integer variable n.
Note that the null charcter „\0‟ available at the end of a string is not counted.
strcpy(city, “BANGALORE”) ;
This will assign the string “BANGALORE” to the character variable city.
(III)strcat( ) Function:
strcat( ) function is used to join character, Strings. When two character strings are
joined, it is referred as concatenation of strings.
Ex:
city pin
strcat , ;
L R
This will join the two strings and store the result in city as “BANGALORE – 560001”.
Note that the resulting string is always stored in the left side string variable.
• It returns a 0 when two strings are identical. Otherwise it returns a numerical value
which is the different in ASCII values of the first mismatching character of the strings being
compared.
• This will return an integer value „- 10‟ which is the difference in the ASCII values of
the first mismatching letters „D‟ and „N‟
• Note that the integer value obtained as the difference may be assigned to an integer
variable as follows:
int n;
n = strcmp(city, town);
• We use scanf ( ) function to read strings which do not have any white spaces. Ex:
scanf(“ %s ”, city );
• When this statement is executed, the user has to enter the city name
(eg “NEWDELHI”) without any white space (i.e not like “NEW DELHI”).
• The white space in the string will terminate the reading and only “NEW” is assigned to
city.
• To read a string with white spaces gets( ) function or a loop can be used as.
(i) gets(city);
do
{
Ch = getchar( );
city[ i ] = ch; i++;
while(ch ! = „ \n ‟) ;
• The copy or assign a single character to a string variable, the assignment can be written
as given below;
city[ i ] = „N‟;
• When a string contains more than one character, the assignment is written as strcpy
(city, “NEW DELHI”);
atoi( ) Function:
n = atoi(st);
This will assign the integer value 24175 to the integer variable n.
(1) Write a C program to count the occurrence of a particular character in the given string.
Solution:
Program:
count = 0;
• When this program is executed, the user has to enter the string and the character to be
counted in the given string.
Output:
Alternative Method:
• This program can also be written to compare the first character of the string with the
last, second with the second last and so on up to the middle of the string as shown.
01234567 8
HY ER
• If all the characters are identical, then the string is a palindrome string.
(2) Write a C program to compare two strings which are given as input through keyboard
and print the alphabetically greater string.
Solution
The alphabetically greater string is the one whose ASCII value of the first letter is
greater than that of the second string.
St1 = “ALPHA”
St2 = “BEETA”
St2 is greater than st1 because the ASCII value of „B‟ in “BETA” is greater than that
of „A‟ in „ALPHA‟.
• Note that when the first letters of the two strings are identical, the second letters of
the strings are compared.
/* PROGRAM TO PRINT ALPHABETICALLY GREATER STRING * /
# include<stdio.h>
# include<conio.h>
#include<string.h>
main( )
clrscr( );
• When this program is executed, the user has to enter the two strings.
• Note that strcmp( ) function returns a positive value when string 1 is greater & a
negative value when string 2 is greater.
Output:
# include<stdio.h>
# include<conio.h>
#
include<string.h>
main( )
clrscr( ):
scanf(“%d”, &n);
/* loop to print the alphabetical list of names * / printf (“ \n names in alphabetical order”);
getch( );
• When this program is executed, the user has to first enter the total no of names
Output:
SHERI
N
SONIK
A
ARUN
DEEPAK
SHERIN
SONIKA
(3) Write a C program to convert a line in the lower case text to upper case.
Solution
* Note that the difference between the lower and upper case letters (ie. – 32) is used
for conversion.
# include<stdio.h>
# include<conio.h>
#
include<string.h>
main( )
char
st[80]; int
i; clrscr( );
OUTPUT:
Enter a sentence:
Some „C‟ compilers will accept the following string handling functions which
are available in header files string.h and ctype.h
Functions:
(i) strupr( ): to convert all alphabets in a string to upper case letters. Ex:
(ii) strlwr( ): To convert all alphabets in a string to lower case letters. Ex:
m = -4
(v) strcmpi( ): To compare two strings with case in sensitive (neglecting upper
/ lower case)
Ex:
s1 will be “NewDel”.
C language also supports operations with characters and these functions are
available in the header file “ctype.h”.
* Note that the value „1‟ or positive integer will be returned when acondition is
true, and value „0‟ will be returned when condition is false.
Function:
isupper(“A‟) 1
isupper(“a‟) 0
isupper(“8‟) 0
islower(“n‟) 1
isalpha(“K‟) 1
isalpha(“+‟) 0
isalnum(“y‟) 1
isalnum(“-‟) 0
isdigit(“6‟) 1
isdigit(“a‟) 0
(vi) isxdigit( ) : To test if a character is a Hexa decimal number (0-9, A-F and a-
f are Hexa decimal digits)
Ex:
isxdigit(“9‟) 1
isxdigit(“A‟) 1
isxdigit(“M‟) 0
tolower(“A‟) a
toupper(“a‟) A.
Functions:-
Designing:-
Functions are an essential ingredient of all programs, large and small, and serve as our primary
medium to express computational processes ina programming language. So far, we have
discussed the formal properties of functions and how they are applied. We now turn to the topic
of what makes a good function. Fundamentally, the qualities of good functions all reinforce the
idea that functions are abstractions.
Each function should have exactly one job. That job should be identifiable with a
short name and characterizable in a single line oftext. Functions that perform multiple
jobs in sequence should be divided into multiple functions.
These guidelines improve the readability of code, reduce the number of errors, and often
minimize the total amount of code written.
Decomposing a complex task into concise functions is a skill that takesexperience to master.
Fortunately, Python provides several features tosupport your efforts.
Structured programs:-
Function in C:-
A function is a block of statements that performs a specific task. Suppose you are building an
application in C language and in one of your program, you need to perform a same task more
than once. In suchcase you have two options –
a) Use the same set of statements every time you want to perform thetask
b) Create a function to perform that task, and just call it every time youneed to perform
that task.
Using option (b) is a good practice and a good programmer always usesfunctions while writing
codes in C.
Types of functions
Inter-Function Communication:-
When a function gets executed in the program, the execution control is transferred from calling a
function to called function and executes function definition, and finally comes back to the calling
function. In this process, both calling and called functions have to communicate witheach
other to exchange information. The process of exchanging information between calling and called
functions is called inter-function communication.
Downward Communication
Upward Communication
Bi-directional Communication
The standard library functions are built-in functions in C programming.These functions are
screen (display output on the screen). This function is defined inthe stdio.h header file.
Hence, to use the printf() function, we need to include the stdio.h headerfile using #include
<stdio.h>.
The sqrt() function calculates the square root of a number. The functionis defined in the math.h
header file.
Just like variables, array can also be passed to a function as an argument. In this guide, we will
learn how to pass the array to a functionusing call by value and call by reference methods.
C – Array
Function call by value in C Function
call by reference in C
As we already know in this type of function call, the actual parameter iscopied to the formal
parameters.
#include<stdio.h>
voiddisp(charch)
printf("%c ",ch);
int main()
return0;
Output:
abcdefghij
When we pass the address of an array while calling a function then this is called function call by
reference. When we pass an address as an argument, the function declaration should have a
pointer as a parameterto receive the passed address.
#include<stdio.h>
voiddisp(int*num)
printf("%d ",*num);
}
int main()
{ intarr[]={1,2,3,4,5,6,7,8,9,0};
for(inti=0;i<10;i++)
}
return0;
Output:
1234567890
In the above example, we have passed the address of each array element one by one using a for
loop in C. However you can also pass anentire array to a function like this:
Note: The array name itself is the address of first element of that array.For example if array
name is arr then you can say that arr is equivalentto the &arr[0].
#include<stdio.h>
voidmyfuncn(int*var1,int var2)
* the array and the var2 is the size of the array. In the
*/
int main()
{ intvar_arr[]={11,22,33,44,55,66,77};
myfuncn(var_arr,7);
return0;
Output:
Value of var_arr[0]is:11Value of
var_arr[1]is:22 Value of
var_arr[2]is:33 Value of
var_arr[3]is:44 Value of
var_arr[4]is:55 Value of
var_arr[5]is:66
Value of var_arr[6]is:77
#include<stdio.h>
voidsalaryhike(int *var,
int b)
*var = *var+b;
int main()
printf("Enter bonus:");
scanf("%d", &bonus);
salaryhike(&salary, bonus);
printf("Final salary: %d",
salary); return0;
Output: