際際滷

際際滷Share a Scribd company logo
STRING MANIPULATIONS IN C
In C language, an array of characters is known as a string. char
st[80];
This statement declares a string array with 80 characters.
The control character 0 which represents a null character is placed automatically at the
end of any string used.
STRING HANDLING FUNCTIONS IN C:
There are four important string Handling functions in C language.
(i) strlen( ) function
(ii) strcpy( ) function
(iii) strcat( ) function
(iv) strcmp( ) function
(I) strlen( )Function:
strlen( ) function is used to find the length of a character string. Ex:
int n;
char st[20] = Bangalore; n
= strlen(st);
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.
(II) strcpy( )Function:
strcpy( ) function is used to copy from one string to another string. Ex :
L R
char city[15];
strcpy(city, BANGALORE) ;
This will assign the string BANGALORE to the character variable city.
* Note that character value like
city = BANGALORE; cannot be assigned in C language.
(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:
char city[20] = BANGALORE;
char pin[8] = -560001;
strcat
city
,
pin
;
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.
(iv) strcmp( ) Function:
strcm ( ) function is used to compare two character strings.
 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.
char city[20] = Madras; char
town[20] = Mangalore;
strcmp(city, town);
 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);
READING / WRITING STRINGS:
 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);
(ii) do  while loop i
= 0;
do
{
Ch = getchar( );
city[ i ] = ch; i++;
}
while(ch ! =  n ) ;
i - - ; city[ i ] = 0;
 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:
atoi( ) function is a C library function which is used to convert a string of digits
to the integer value.
char st[10] = 24175 ;
int n;
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:
Consider a string MISSISSIPPI. Count the appearance of a character, say S.
Program:
* program to count a character in a string * / #
include<stdio.h>
# include<conio.h> #
include<string.h>
main( )
{
char st[20], ch;
int count, l,i;
clrscr( );
printf( n Enter the string:);
gets(st);
printf( n which char to be counted ?);
scanf( %c, &ch);
/* loop to check the occurrence of the character * / l =
strlen(st);
count = 0;
for( i=0; i < l; i++)
if(st[ i ] = = ch)
count ++;
printf( n the character %c occurs %d times, ch, count);
getch( ) ;
}
 When this program is executed, the user has to enter the string and the character to be
counted in the given string.
Output:
Enter the string : MISSISSIPPI
Which char to be counted? S
The character S occurs 4 times.
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.
0 1 2 3 4 5 6 7 8
 If all the characters are identical, then the string is a palindrome string.
 Otherwise the loop will be terminated.
(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.
Consider the two character strings.
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.
E R
H Y
/* PROGRAM TO PRINT ALPHABETICALLY GREATER STRING * /
# include<stdio.h>
# include<conio.h>
#include<string.h>
main( )
{
char st1[20], st2[20];
clrscr( );
printf( n Enter string:);
scanf(  %s ,st1);
printf( n Enter string 2:);
scanf(  %s , st2);
if(strcmp(st1,st2)> 0)
printf(n %s is alphabetically greater string, st1);
else
printf( n %s is alphabetically greater string, st2);
getch( );
}
 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:
Enter string 1 : ACPHA
Enter string 2 : BETA
BETA is alphabetically greater string.
PROGRAM TO ARRANGE NAMES IN ALPHABETICAL ORDER.
# include<stdio.h>
# include<conio.h>
#
include<string.h>
main( )
{
char names[50][20], temp[20]; int
n,i,j;
clrscr( ):
printf( n How many names?);
scanf(%d, &n);
printf( n Enter the %d names one by one  n,n);
for( i=0; i<n; i++)
scanf(%s, names[ i ]);
/* loop to arrange names in alphabetical order * /
for( i=0; i<n-1; i++)
for ( j =i+1; j<n; j++)
if(strcmp(names[ i ], names[ j ]) > 0)
{
strcpy( temp, names[ i ]); strcpy(names[ i ], names[ j ]);
strcpy(names[ j ],temp);
/* loop to print the alphabetical list of names * / printf ( n names in alphabetical order);
for( i=0; i<n; i++)
printf(n %s, names[ i ]);
getch( );
}
 When this program is executed, the user has to first enter the total no of names
(n) and then all the names in the list.
 The names are compared, rearranged and printed in alphabetical order.
Output:
How many names? 4
Enter the 4 names one by one
DEEPAK
SHERI
N
SONIK
A
ARUN
Names in Alphabetical order
ARUN
DEEPAK
SHERIN
SONIKA
(3) Write a C program to convert a line in the lower case text to upper case.
Solution
Consider ASCII values of lower and upper case letters A
 65 B  66..................................... Z 
90
a - 97 b  98 ......................................z  122
* Note that the difference between the lower and upper case letters (ie.  32) is used
for conversion.
/* PROGRAM TO CONVERT LOWER CASE TEST TO UPPER CASE * /
# include<stdio.h>
# include<conio.h>
#
include<string.h>
main( )
{
char
st[80]; int
i; clrscr( );
printf(  n Enter a sentence :  n);
gets(st);
/* loop to convert lower case alphabet to upper case * / for(
i=0; i<strlen(st); i++)
if(st[ i ] >= a && st[ i ] <= z)st[ i ] = st[ i ]  32;
printf( n the converted upper case strings n %s, st);
getch( );
}
OUTPUT:
Enter a sentence:
Logical thinking is a must to learn programming
The converted upper case string is
Additional String Handling Functions:
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:
strupr( delhi )  DELHI
(ii) strlwr( ): To convert all alphabets in a string to lower case letters. Ex:
strlwr( CITY ) city
(iii) strrev( ): To reverse a string
Ex: strrev( SACHIN ) NIHCAS
(iv) strncmp( ): To compare first n characters of two strings. Ex:
m = strncmp ( DELHI ,  DIDAR , 2);
m = -4
(v) strcmpi( ): To compare two strings with case in sensitive (neglecting upper
/ lower case)
Ex:
m=strcmpi( DELHI ,  delhi ); m = 0.
(vi) strncat( ): To join specific number of letters to another string. Ex.
char s1[10] = New; char
s2[10] = Delhi -41;
strncat(s1,s2,3);
s1 will be NewDel.
Operations with Characters:
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.
 Some ctype.h functions are listed below.
Function:
(i) isupper( ) : To test if a character is a upper case letters. Ex:
isupper(A) 1
isupper(a) 0
isupper(8) 0
(ii) islower( ) : To test if a charcter is a lower case letter.
Ex:
islower(n) 1
(iii)isalpha( ) : To test if a character is an alphabet. Ex:
isalpha(K) 1
isalpha(+) 0
(iv) isalnum( ) : To test if a character is an alphabet or number. Ex:
isalnum(8) 1
isalnum(y) 1
isalnum(-) 0
(v)isdigit( ) : To test if a character is a number. Ex:
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
(vii) tolower( ) : To convert an alphabet to lower case letter Ex:
tolower(A) a
(viii)toupper( ) : To convert an alphabet to upper case letter Ex:
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.
Don't repeat yourself is a central tenet of software engineering. Theso-called DRY
principle states that multiple fragments of code should not describe redundant logic.
Instead, that logic should be implemented once, given a name, and applied multiple
times. If youfind yourself copying and pasting a block of code, you have probably
found an opportunity for functional abstraction.
Functions should be defined generally. Squaring is not in thePython Library
precisely because it is a special case of
the pow function, which raises numbers to arbitrary powers.
These guidelines improve the readability of code, reduce the number oferrors, 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:-
Structured programming is a programming paradigm aimed atimproving the clarity,
quality, and development time of a
computer program by making extensive use of the structured controlflow constructs of
selection (if/then/else) and repetition (while and for), block structures, and subroutines.
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
1) Predefined standard library functions  such
as puts(), gets(), printf(), scanf() etc  These are the functions which already have a definition
in header files (.h files like stdio.h), so we justcall them whenever there is a need to use them.
 2) User Defined functions  The functions that we create in aprogram are
known as user defined 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.
In C, the inter function communication is classified as follows...
Downward Communication
Upward Communication
Bi-directional Communication
Standard library functions:-
The standard library functions are built-in functions in C programming.These functions are
defined in header files. For example,
The printf() is a standard library function to send formatted output to the
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.
Passing Array to Functions:-
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.
To understand this guide, you should have the knowledge offollowing C
Programming topics:
C  Array
Function call by value in C Function
call by reference in C
Passing array to function using call by value method
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()
{
chararr[]={'a','b','c','d','e','f','g','h','i','j'}; for(int x=0;
x<10; x++)
{
/* Im passing each element one by one using subscript*/disp(arr[x]);
}
return0;
}
Output:
a b c d e f g h i j
Passing array to function using call by reference
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++)
{
/* Passing addresses of array elements*/disp(&arr[i]);
}
return0;
}
Output:
1234567890
How to pass an entire array to a function as an argument?
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 pointer var1 is pointing to the first element of
* the array and the var2 is the size of the array. In the
* loop we are incrementing pointer so that it points to
* the next element of the array on each increment.
*
*/
for(int x=0; x<var2; x++)
{
printf("Value of var_arr[%d] is: %d n", x,*var1);
/*increment pointer for next element fetch*/var1++;
}
}
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
Passing pointer to function:-
In this example, we are passing a pointer to a function. When we pass apointer as an
argument instead of a variable then the address of the variable is passed instead of the value.
So any change made by the function using the pointer is permanently made at the address of
passedvariable. This technique is known as call by reference in C.
#include<stdio.h>
voidsalaryhike(int *var,
int b)
{
*var = *var+b;
}
int main()
{
int salary=0, bonus=0;
printf("Enter the employee current
salary:");scanf("%d", &salary);
printf("Enter bonus:");
scanf("%d", &bonus);
salaryhike(&salary, bonus);
printf("Final salary: %d",
salary);return0;
}
Output:
Enter the employee current
salary:10000Enter bonus:2000
Final salary: 12000

More Related Content

Similar to PROBLEM SOLVING USING A PPSC- UNIT -3.pdf (20)

Strings in c++
Strings in c++Strings in c++
Strings in c++
International Islamic University
String notes
String notesString notes
String notes
Prasadu Peddi
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
Principals of Programming in CModule -5.pdfModule-4.pdf
Principals of Programming in CModule -5.pdfModule-4.pdfPrincipals of Programming in CModule -5.pdfModule-4.pdf
Principals of Programming in CModule -5.pdfModule-4.pdf
anilcsbs
14 strings
14 strings14 strings
14 strings
Rohit Shrivastava
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
Rai University
strings
stringsstrings
strings
teach4uin
Strings
StringsStrings
Strings
Dhiviya Rose
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
Rai University
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
Rai University
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
aneebkmct
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
Rai University
Strings and pointers
Strings and pointersStrings and pointers
Strings and pointers
Gurpreet Singh Sond
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
Rai University
UNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledgeUNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledge
2024163103shubham
Strings
StringsStrings
Strings
Imad Ali
string in C
string in Cstring in C
string in C
CGC Technical campus,Mohali
Cse115 lecture14strings part01
Cse115 lecture14strings part01Cse115 lecture14strings part01
Cse115 lecture14strings part01
Md. Ashikur Rahman
C string
C stringC string
C string
University of Potsdam
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
Hemantha Kulathilake
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
Principals of Programming in CModule -5.pdfModule-4.pdf
Principals of Programming in CModule -5.pdfModule-4.pdfPrincipals of Programming in CModule -5.pdfModule-4.pdf
Principals of Programming in CModule -5.pdfModule-4.pdf
anilcsbs
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
Rai University
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
Rai University
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
Rai University
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
aneebkmct
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
Rai University
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
Rai University
UNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledgeUNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledge
2024163103shubham
Strings
StringsStrings
Strings
Imad Ali
Cse115 lecture14strings part01
Cse115 lecture14strings part01Cse115 lecture14strings part01
Cse115 lecture14strings part01
Md. Ashikur Rahman

More from JNTUK KAKINADA (17)

LECTURE NOTES ON SOLAR, NUCLEAR, WIND POWER ENERGY
LECTURE NOTES ON SOLAR, NUCLEAR, WIND POWER ENERGYLECTURE NOTES ON SOLAR, NUCLEAR, WIND POWER ENERGY
LECTURE NOTES ON SOLAR, NUCLEAR, WIND POWER ENERGY
JNTUK KAKINADA
transistors, amplifiers and oscillators information
transistors, amplifiers and oscillators informationtransistors, amplifiers and oscillators information
transistors, amplifiers and oscillators information
JNTUK KAKINADA
electrical circuits and basic RLC parameters
electrical circuits and basic RLC parameterselectrical circuits and basic RLC parameters
electrical circuits and basic RLC parameters
JNTUK KAKINADA
dc generators,transformers, moving coil and moving Iron instruments
dc generators,transformers, moving coil and moving Iron instrumentsdc generators,transformers, moving coil and moving Iron instruments
dc generators,transformers, moving coil and moving Iron instruments
JNTUK KAKINADA
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdfPROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
PROBLEM SOLVING USING PPSC- UNIT -4.pdf
PROBLEM SOLVING USING  PPSC- UNIT -4.pdfPROBLEM SOLVING USING  PPSC- UNIT -4.pdf
PROBLEM SOLVING USING PPSC- UNIT -4.pdf
JNTUK KAKINADA
PROBLEM SOLVING USING C PPSC- UNIT -5.pdf
PROBLEM SOLVING USING C PPSC- UNIT -5.pdfPROBLEM SOLVING USING C PPSC- UNIT -5.pdf
PROBLEM SOLVING USING C PPSC- UNIT -5.pdf
JNTUK KAKINADA
introduction to programming using ANSI C
introduction to programming using ANSI Cintroduction to programming using ANSI C
introduction to programming using ANSI C
JNTUK KAKINADA
DESCRIPTION AND EXPLANATION ON AC CIRCUITS.pptx
DESCRIPTION AND EXPLANATION ON AC CIRCUITS.pptxDESCRIPTION AND EXPLANATION ON AC CIRCUITS.pptx
DESCRIPTION AND EXPLANATION ON AC CIRCUITS.pptx
JNTUK KAKINADA
classification of Induction motor and their explanation .pptx
classification of Induction motor and their explanation .pptxclassification of Induction motor and their explanation .pptx
classification of Induction motor and their explanation .pptx
JNTUK KAKINADA
Total enegy forecasting using deep learning
Total enegy forecasting using deep learningTotal enegy forecasting using deep learning
Total enegy forecasting using deep learning
JNTUK KAKINADA
A critical review of energy forecasting methods
A critical review of energy forecasting methodsA critical review of energy forecasting methods
A critical review of energy forecasting methods
JNTUK KAKINADA
AI R16 - UNIT-2.pdf
AI R16 - UNIT-2.pdfAI R16 - UNIT-2.pdf
AI R16 - UNIT-2.pdf
JNTUK KAKINADA
AI R16 - UNIT-4.pdf
AI R16 - UNIT-4.pdfAI R16 - UNIT-4.pdf
AI R16 - UNIT-4.pdf
JNTUK KAKINADA
AI R16 - UNIT-1.pdf
AI R16 - UNIT-1.pdfAI R16 - UNIT-1.pdf
AI R16 - UNIT-1.pdf
JNTUK KAKINADA
AI R16 - UNIT-5.pdf
AI R16 - UNIT-5.pdfAI R16 - UNIT-5.pdf
AI R16 - UNIT-5.pdf
JNTUK KAKINADA
AI R16 - UNIT-3.pdf
AI R16 - UNIT-3.pdfAI R16 - UNIT-3.pdf
AI R16 - UNIT-3.pdf
JNTUK KAKINADA
LECTURE NOTES ON SOLAR, NUCLEAR, WIND POWER ENERGY
LECTURE NOTES ON SOLAR, NUCLEAR, WIND POWER ENERGYLECTURE NOTES ON SOLAR, NUCLEAR, WIND POWER ENERGY
LECTURE NOTES ON SOLAR, NUCLEAR, WIND POWER ENERGY
JNTUK KAKINADA
transistors, amplifiers and oscillators information
transistors, amplifiers and oscillators informationtransistors, amplifiers and oscillators information
transistors, amplifiers and oscillators information
JNTUK KAKINADA
electrical circuits and basic RLC parameters
electrical circuits and basic RLC parameterselectrical circuits and basic RLC parameters
electrical circuits and basic RLC parameters
JNTUK KAKINADA
dc generators,transformers, moving coil and moving Iron instruments
dc generators,transformers, moving coil and moving Iron instrumentsdc generators,transformers, moving coil and moving Iron instruments
dc generators,transformers, moving coil and moving Iron instruments
JNTUK KAKINADA
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdfPROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
PROBLEM SOLVING USING PPSC- UNIT -4.pdf
PROBLEM SOLVING USING  PPSC- UNIT -4.pdfPROBLEM SOLVING USING  PPSC- UNIT -4.pdf
PROBLEM SOLVING USING PPSC- UNIT -4.pdf
JNTUK KAKINADA
PROBLEM SOLVING USING C PPSC- UNIT -5.pdf
PROBLEM SOLVING USING C PPSC- UNIT -5.pdfPROBLEM SOLVING USING C PPSC- UNIT -5.pdf
PROBLEM SOLVING USING C PPSC- UNIT -5.pdf
JNTUK KAKINADA
introduction to programming using ANSI C
introduction to programming using ANSI Cintroduction to programming using ANSI C
introduction to programming using ANSI C
JNTUK KAKINADA
DESCRIPTION AND EXPLANATION ON AC CIRCUITS.pptx
DESCRIPTION AND EXPLANATION ON AC CIRCUITS.pptxDESCRIPTION AND EXPLANATION ON AC CIRCUITS.pptx
DESCRIPTION AND EXPLANATION ON AC CIRCUITS.pptx
JNTUK KAKINADA
classification of Induction motor and their explanation .pptx
classification of Induction motor and their explanation .pptxclassification of Induction motor and their explanation .pptx
classification of Induction motor and their explanation .pptx
JNTUK KAKINADA
Total enegy forecasting using deep learning
Total enegy forecasting using deep learningTotal enegy forecasting using deep learning
Total enegy forecasting using deep learning
JNTUK KAKINADA
A critical review of energy forecasting methods
A critical review of energy forecasting methodsA critical review of energy forecasting methods
A critical review of energy forecasting methods
JNTUK KAKINADA
AI R16 - UNIT-2.pdf
AI R16 - UNIT-2.pdfAI R16 - UNIT-2.pdf
AI R16 - UNIT-2.pdf
JNTUK KAKINADA
AI R16 - UNIT-4.pdf
AI R16 - UNIT-4.pdfAI R16 - UNIT-4.pdf
AI R16 - UNIT-4.pdf
JNTUK KAKINADA
AI R16 - UNIT-1.pdf
AI R16 - UNIT-1.pdfAI R16 - UNIT-1.pdf
AI R16 - UNIT-1.pdf
JNTUK KAKINADA
AI R16 - UNIT-5.pdf
AI R16 - UNIT-5.pdfAI R16 - UNIT-5.pdf
AI R16 - UNIT-5.pdf
JNTUK KAKINADA
AI R16 - UNIT-3.pdf
AI R16 - UNIT-3.pdfAI R16 - UNIT-3.pdf
AI R16 - UNIT-3.pdf
JNTUK KAKINADA

Recently uploaded (20)

PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINESPATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
BananaIP Counsels
PUBH1000 際際滷s - Module 7: Ecological Health
PUBH1000 際際滷s - Module 7: Ecological HealthPUBH1000 際際滷s - Module 7: Ecological Health
PUBH1000 際際滷s - Module 7: Ecological Health
Jonathan Hallett
Easier-to-Save.Nest report into workplace saving
Easier-to-Save.Nest report into workplace savingEasier-to-Save.Nest report into workplace saving
Easier-to-Save.Nest report into workplace saving
Henry Tapper
Anthelmintic Agent.pptx by Mrs. Manjushri P. Dabhade
Anthelmintic Agent.pptx by Mrs. Manjushri P. DabhadeAnthelmintic Agent.pptx by Mrs. Manjushri P. Dabhade
Anthelmintic Agent.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
Developing Topic and Research Question for Systematic Reviews - Emmanuel Ekpor
Developing Topic and Research Question for Systematic Reviews - Emmanuel EkporDeveloping Topic and Research Question for Systematic Reviews - Emmanuel Ekpor
Developing Topic and Research Question for Systematic Reviews - Emmanuel Ekpor
Systematic Reviews Network (SRN)
How to Invoice Shipping Cost to Customer in Odoo 17
How to Invoice Shipping Cost to Customer in Odoo 17How to Invoice Shipping Cost to Customer in Odoo 17
How to Invoice Shipping Cost to Customer in Odoo 17
Celine George
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
Prabhakar Singh Patel
"The Write Path: Navigating Research Writing, Publication, and Professional G...
"The Write Path: Navigating Research Writing, Publication, and Professional G..."The Write Path: Navigating Research Writing, Publication, and Professional G...
"The Write Path: Navigating Research Writing, Publication, and Professional G...
neelottama
technology in banking ppt FOR E-CONTENT -2.ppt
technology in banking ppt  FOR E-CONTENT -2.ppttechnology in banking ppt  FOR E-CONTENT -2.ppt
technology in banking ppt FOR E-CONTENT -2.ppt
HARIHARAN A
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. DabhadeSynthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
nature and importance of Indian Knowledge System
nature and importance of Indian Knowledge Systemnature and importance of Indian Knowledge System
nature and importance of Indian Knowledge System
hanishabatra0
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Sue Beckingham
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-6-2025 ver 5.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-6-2025 ver 5.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-6-2025 ver 5.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-6-2025 ver 5.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdfIB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
Dr. Mahtab Alam
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
A Biodiction : A Unit of Dr. Divya Sharma
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean ExpressionsIntroduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
GS Virdi
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. DabhadeCombinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
GenAI for Trading and Asset Management by Ernest Chan
GenAI for Trading and Asset Management by Ernest ChanGenAI for Trading and Asset Management by Ernest Chan
GenAI for Trading and Asset Management by Ernest Chan
QuantInsti
How to configure the retail shop in Odoo 17 Point of Sale
How to configure the retail shop in Odoo 17 Point of SaleHow to configure the retail shop in Odoo 17 Point of Sale
How to configure the retail shop in Odoo 17 Point of Sale
Celine George
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptxO SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
Literature Hero
PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINESPATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
PATENTABILITY UNDER THE 2025 CRI DRAFT GUIDELINES
BananaIP Counsels
PUBH1000 際際滷s - Module 7: Ecological Health
PUBH1000 際際滷s - Module 7: Ecological HealthPUBH1000 際際滷s - Module 7: Ecological Health
PUBH1000 際際滷s - Module 7: Ecological Health
Jonathan Hallett
Easier-to-Save.Nest report into workplace saving
Easier-to-Save.Nest report into workplace savingEasier-to-Save.Nest report into workplace saving
Easier-to-Save.Nest report into workplace saving
Henry Tapper
Anthelmintic Agent.pptx by Mrs. Manjushri P. Dabhade
Anthelmintic Agent.pptx by Mrs. Manjushri P. DabhadeAnthelmintic Agent.pptx by Mrs. Manjushri P. Dabhade
Anthelmintic Agent.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
Developing Topic and Research Question for Systematic Reviews - Emmanuel Ekpor
Developing Topic and Research Question for Systematic Reviews - Emmanuel EkporDeveloping Topic and Research Question for Systematic Reviews - Emmanuel Ekpor
Developing Topic and Research Question for Systematic Reviews - Emmanuel Ekpor
Systematic Reviews Network (SRN)
How to Invoice Shipping Cost to Customer in Odoo 17
How to Invoice Shipping Cost to Customer in Odoo 17How to Invoice Shipping Cost to Customer in Odoo 17
How to Invoice Shipping Cost to Customer in Odoo 17
Celine George
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
URINE SPECIMEN COLLECTION AND HANDLING CLASS 1 FOR ALL PARAMEDICAL OR CLINICA...
Prabhakar Singh Patel
"The Write Path: Navigating Research Writing, Publication, and Professional G...
"The Write Path: Navigating Research Writing, Publication, and Professional G..."The Write Path: Navigating Research Writing, Publication, and Professional G...
"The Write Path: Navigating Research Writing, Publication, and Professional G...
neelottama
technology in banking ppt FOR E-CONTENT -2.ppt
technology in banking ppt  FOR E-CONTENT -2.ppttechnology in banking ppt  FOR E-CONTENT -2.ppt
technology in banking ppt FOR E-CONTENT -2.ppt
HARIHARAN A
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. DabhadeSynthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Synthesis for VIth SEM 21-2-25.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
nature and importance of Indian Knowledge System
nature and importance of Indian Knowledge Systemnature and importance of Indian Knowledge System
nature and importance of Indian Knowledge System
hanishabatra0
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Enhancing SoTL through Generative AI -- Opportunities and Ethical Considerati...
Sue Beckingham
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdfIB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
IB-Unit-4 BBA BVIMR 2022 Syllabus_watermark.pdf
Dr. Mahtab Alam
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
Bioinformatics: History of Bioinformatics, Components of Bioinformatics, Geno...
A Biodiction : A Unit of Dr. Divya Sharma
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean ExpressionsIntroduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
Introduction to Karnaugh Maps (K-Maps) for Simplifying Boolean Expressions
GS Virdi
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. DabhadeCombinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Combinatorial_Chemistry.pptx by Mrs. Manjushri P. Dabhade
Dabhade madam Dabhade
GenAI for Trading and Asset Management by Ernest Chan
GenAI for Trading and Asset Management by Ernest ChanGenAI for Trading and Asset Management by Ernest Chan
GenAI for Trading and Asset Management by Ernest Chan
QuantInsti
How to configure the retail shop in Odoo 17 Point of Sale
How to configure the retail shop in Odoo 17 Point of SaleHow to configure the retail shop in Odoo 17 Point of Sale
How to configure the retail shop in Odoo 17 Point of Sale
Celine George
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptxO SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
O SWEET SPONTANEOUS BY EDWARD ESTLIN CUMMINGSAN.pptx
Literature Hero

PROBLEM SOLVING USING A PPSC- UNIT -3.pdf

  • 1. STRING MANIPULATIONS IN C In C language, an array of characters is known as a string. char st[80]; This statement declares a string array with 80 characters. The control character 0 which represents a null character is placed automatically at the end of any string used. STRING HANDLING FUNCTIONS IN C: There are four important string Handling functions in C language. (i) strlen( ) function (ii) strcpy( ) function (iii) strcat( ) function (iv) strcmp( ) function (I) strlen( )Function: strlen( ) function is used to find the length of a character string. Ex: int n; char st[20] = Bangalore; n = strlen(st); 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. (II) strcpy( )Function: strcpy( ) function is used to copy from one string to another string. Ex :
  • 2. L R char city[15]; strcpy(city, BANGALORE) ; This will assign the string BANGALORE to the character variable city. * Note that character value like city = BANGALORE; cannot be assigned in C language. (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: char city[20] = BANGALORE; char pin[8] = -560001; strcat city , pin ; 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. (iv) strcmp( ) Function: strcm ( ) function is used to compare two character strings. 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
  • 3. compared. char city[20] = Madras; char town[20] = Mangalore; strcmp(city, town); 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); READING / WRITING STRINGS: 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); (ii) do while loop i = 0; do {
  • 4. Ch = getchar( ); city[ i ] = ch; i++; } while(ch ! = n ) ; i - - ; city[ i ] = 0; 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: atoi( ) function is a C library function which is used to convert a string of digits to the integer value. char st[10] = 24175 ; int n; 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: Consider a string MISSISSIPPI. Count the appearance of a character, say S. Program: * program to count a character in a string * / # include<stdio.h>
  • 5. # include<conio.h> # include<string.h> main( ) { char st[20], ch; int count, l,i; clrscr( ); printf( n Enter the string:); gets(st); printf( n which char to be counted ?); scanf( %c, &ch); /* loop to check the occurrence of the character * / l = strlen(st); count = 0; for( i=0; i < l; i++) if(st[ i ] = = ch) count ++; printf( n the character %c occurs %d times, ch, count); getch( ) ; } When this program is executed, the user has to enter the string and the character to be counted in the given string. Output: Enter the string : MISSISSIPPI
  • 6. Which char to be counted? S The character S occurs 4 times. 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. 0 1 2 3 4 5 6 7 8 If all the characters are identical, then the string is a palindrome string. Otherwise the loop will be terminated. (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. Consider the two character strings. 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. E R H Y
  • 7. /* PROGRAM TO PRINT ALPHABETICALLY GREATER STRING * / # include<stdio.h> # include<conio.h> #include<string.h> main( ) { char st1[20], st2[20]; clrscr( ); printf( n Enter string:); scanf( %s ,st1); printf( n Enter string 2:); scanf( %s , st2); if(strcmp(st1,st2)> 0) printf(n %s is alphabetically greater string, st1); else printf( n %s is alphabetically greater string, st2); getch( ); } 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: Enter string 1 : ACPHA Enter string 2 : BETA
  • 8. BETA is alphabetically greater string. PROGRAM TO ARRANGE NAMES IN ALPHABETICAL ORDER. # include<stdio.h> # include<conio.h> # include<string.h> main( ) { char names[50][20], temp[20]; int n,i,j; clrscr( ): printf( n How many names?); scanf(%d, &n); printf( n Enter the %d names one by one n,n); for( i=0; i<n; i++) scanf(%s, names[ i ]); /* loop to arrange names in alphabetical order * / for( i=0; i<n-1; i++) for ( j =i+1; j<n; j++) if(strcmp(names[ i ], names[ j ]) > 0) { strcpy( temp, names[ i ]); strcpy(names[ i ], names[ j ]); strcpy(names[ j ],temp); /* loop to print the alphabetical list of names * / printf ( n names in alphabetical order); for( i=0; i<n; i++)
  • 9. printf(n %s, names[ i ]); getch( ); } When this program is executed, the user has to first enter the total no of names (n) and then all the names in the list. The names are compared, rearranged and printed in alphabetical order. Output: How many names? 4 Enter the 4 names one by one DEEPAK SHERI N SONIK A ARUN Names in Alphabetical order ARUN DEEPAK SHERIN SONIKA (3) Write a C program to convert a line in the lower case text to upper case. Solution Consider ASCII values of lower and upper case letters A 65 B 66..................................... Z 90
  • 10. a - 97 b 98 ......................................z 122 * Note that the difference between the lower and upper case letters (ie. 32) is used for conversion. /* PROGRAM TO CONVERT LOWER CASE TEST TO UPPER CASE * / # include<stdio.h> # include<conio.h> # include<string.h> main( ) { char st[80]; int i; clrscr( ); printf( n Enter a sentence : n); gets(st); /* loop to convert lower case alphabet to upper case * / for( i=0; i<strlen(st); i++) if(st[ i ] >= a && st[ i ] <= z)st[ i ] = st[ i ] 32; printf( n the converted upper case strings n %s, st); getch( ); } OUTPUT: Enter a sentence: Logical thinking is a must to learn programming The converted upper case string is
  • 11. Additional String Handling Functions: 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: strupr( delhi ) DELHI (ii) strlwr( ): To convert all alphabets in a string to lower case letters. Ex: strlwr( CITY ) city (iii) strrev( ): To reverse a string Ex: strrev( SACHIN ) NIHCAS (iv) strncmp( ): To compare first n characters of two strings. Ex: m = strncmp ( DELHI , DIDAR , 2); m = -4 (v) strcmpi( ): To compare two strings with case in sensitive (neglecting upper / lower case) Ex: m=strcmpi( DELHI , delhi ); m = 0.
  • 12. (vi) strncat( ): To join specific number of letters to another string. Ex. char s1[10] = New; char s2[10] = Delhi -41; strncat(s1,s2,3); s1 will be NewDel. Operations with Characters: 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. Some ctype.h functions are listed below. Function: (i) isupper( ) : To test if a character is a upper case letters. Ex: isupper(A) 1 isupper(a) 0 isupper(8) 0 (ii) islower( ) : To test if a charcter is a lower case letter. Ex: islower(n) 1 (iii)isalpha( ) : To test if a character is an alphabet. Ex: isalpha(K) 1 isalpha(+) 0 (iv) isalnum( ) : To test if a character is an alphabet or number. Ex:
  • 13. isalnum(8) 1 isalnum(y) 1 isalnum(-) 0 (v)isdigit( ) : To test if a character is a number. Ex: 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 (vii) tolower( ) : To convert an alphabet to lower case letter Ex: tolower(A) a (viii)toupper( ) : To convert an alphabet to upper case letter Ex: 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. Don't repeat yourself is a central tenet of software engineering. Theso-called DRY principle states that multiple fragments of code should not describe redundant logic.
  • 14. Instead, that logic should be implemented once, given a name, and applied multiple times. If youfind yourself copying and pasting a block of code, you have probably found an opportunity for functional abstraction.
  • 15. Functions should be defined generally. Squaring is not in thePython Library precisely because it is a special case of the pow function, which raises numbers to arbitrary powers. These guidelines improve the readability of code, reduce the number oferrors, 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:- Structured programming is a programming paradigm aimed atimproving the clarity, quality, and development time of a computer program by making extensive use of the structured controlflow constructs of selection (if/then/else) and repetition (while and for), block structures, and subroutines. 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 1) Predefined standard library functions such as puts(), gets(), printf(), scanf() etc These are the functions which already have a definition in header files (.h files like stdio.h), so we justcall them whenever there is a need to use them. 2) User Defined functions The functions that we create in aprogram are known as user defined functions. Inter-Function Communication:-
  • 16. 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. In C, the inter function communication is classified as follows... Downward Communication Upward Communication Bi-directional Communication Standard library functions:- The standard library functions are built-in functions in C programming.These functions are defined in header files. For example, The printf() is a standard library function to send formatted output to the 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. Passing Array to Functions:- 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. To understand this guide, you should have the knowledge offollowing C Programming topics: C Array
  • 17. Function call by value in C Function call by reference in C Passing array to function using call by value method 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() { chararr[]={'a','b','c','d','e','f','g','h','i','j'}; for(int x=0; x<10; x++) { /* Im passing each element one by one using subscript*/disp(arr[x]); } return0; } Output: a b c d e f g h i j Passing array to function using call by reference 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);
  • 18. } int main() { intarr[]={1,2,3,4,5,6,7,8,9,0}; for(inti=0;i<10;i++) { /* Passing addresses of array elements*/disp(&arr[i]); }
  • 19. return0; } Output: 1234567890 How to pass an entire array to a function as an argument? 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 pointer var1 is pointing to the first element of * the array and the var2 is the size of the array. In the * loop we are incrementing pointer so that it points to * the next element of the array on each increment. * */ for(int x=0; x<var2; x++) { printf("Value of var_arr[%d] is: %d n", x,*var1); /*increment pointer for next element fetch*/var1++; } } int main() { intvar_arr[]={11,22,33,44,55,66,77};
  • 20. 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
  • 21. Value of var_arr[6]is:77 Passing pointer to function:- In this example, we are passing a pointer to a function. When we pass apointer as an argument instead of a variable then the address of the variable is passed instead of the value. So any change made by the function using the pointer is permanently made at the address of passedvariable. This technique is known as call by reference in C. #include<stdio.h> voidsalaryhike(int *var, int b) { *var = *var+b; } int main() { int salary=0, bonus=0; printf("Enter the employee current salary:");scanf("%d", &salary); printf("Enter bonus:"); scanf("%d", &bonus); salaryhike(&salary, bonus); printf("Final salary: %d", salary);return0; } Output: Enter the employee current salary:10000Enter bonus:2000 Final salary: 12000