Professional Documents
Culture Documents
Session 10
Session 10
rA
pt
ec
h
C
en
tre
Session 10
U
Strings
se
O
nl
y
y
Objectives
nl
O
se
Explain string variables and constants
U
Explain pointers to strings
tre
Perform string input/output operations
en
Explain the various string functions
C
Explain how arrays can be passed as arguments
h
to functions
ec
arguments
Fo
nl
O
se
Strings are arrays of characters terminated by the
U
NULL (‘\0’) character.
tre
String variables can be assigned string constants.
en
A string constant is a sequence of characters
C
surrounded by double quotes.
h
ec
nl
O
se
U
A typical string variable declaration is:.
tre
en
char str[10];
C
h
str is a character array variable that can hold a maximum
ec
nl
O
se
String I/O operations are carried out using functions from the
U
standard I/O library called stdio.h
tre
The gets() function is the simplest method of accepting a string
en
through standard input
C
Input characters are accepted till the Enter key is pressed
h
ec
The gets() function replaces the terminating ‘\n’ new line character
with the ‘\0’ character
pt
rA
Syntax :
gets(str);
Fo
y
nl
O
se
The puts() function is used to display a string on the standard output device.
U
Syntax :
tre
puts(str);
en
C
The scanf() and printf() functions are used to accept and display mixed data
types with a single statement.
h
ec
scanf(“%s”, str);
rA
printf(“%s”, str);
Elementary Programming with C/Session 1/ 6 of 20
y
String Functions
nl
O
se
Functions for handling strings are found in the
U
standard header file string.h. Few of the
tre
operations performed by these functions are:
en
• C
Concatenating strings
h
• Comparing strings
ec
nl
O
se
Joins two string values into one.
U
tre
Syntax:
en
C
strcat(str1, str2);
h
ec
nl
O
se
Compares two strings and returns an integer
U
value based on the results of the comparison.
tre
en
Syntax:
C
strcmp(str1, str2);
h
ec
nl
O
se
U
Determines the occurrence of a character in a string.
tre
Syntax:
en
strchr(str, chr);
C
h
The function returns a value:
ec
pt
nl
O
se
U
Copies the value in one string onto another
tre
en
Syntax:
C
strcpy(str1, str2);
h
ec
nl
O
se
U
Determines the length of a string
tre
en
Syntax:
C
strlen(str);
h
ec
length of str
rA
Fo
y
Functions-1
nl
O
se
When an array is passed as an argument to a function,
U
only the address of the array is passed
tre
The array name without the subscripts refers to the
en
address of the array
void main()
C
h
ec
{
int ary[10];
pt
.
rA
.
fn_ary(ary);
Fo
.
.
}
Elementary Programming with C/Session 1/ 13 of 20
Passing Arrays to
y
Functions-2
nl
O
se
#include<stdio.h>
U
void main()
tre
{
int num[5], ctr, sum=0;
en
int sum_arr(int num_arr[]); /* Function declaration */
clrscr(); C
h
ec
scanf("%d", &num[ctr]);
}
Fo
y
Functions-3
nl
O
se
sum=sum_arr(num); /* Invokes the function */
U
printf("\nThe sum of the array is %d", sum);
tre
getch();
en
}
C
int sum_arr(int num_arr[]) /* Function definition */
h
{
ec
int i, total;
pt
total+=num_arr[i];
Fo
y
Functions-4
nl
O
se
Sample output of the program
U
tre
Enter number 1: 5
en
Enter number 2: 10
Enter number 3: 13 C
h
ec
Enter number 4: 26
pt
rA
Enter number 5: 21
Fo
y
to Functions-1
nl
O
se
#include<stdio.h>
U
#include<string.h>
tre
void main()
{
en
char lines[5][20];
int ctr, longctr=0;
C
int longest(char lines_arr[][20]);
h
/* Function declaration */
ec
clrscr();
pt
for(ctr=0;ctr<5;ctr++)
rA
y
to Functions-2
nl
O
se
longctr=longest(lines);
U
/* Passes the array to the function */
tre
printf("\nThe longest string is %s", lines[longctr]);
en
getch();
}
C
h
int longest(char lines_arr[][20]) /* Function definition */
ec
{
int i=0, l_ctr=0, prev_len, new_len;
pt
rA
prev_len=strlen(lines_arr[i]);
/* Determines the length of the first element */
Fo
y
to Functions-3
nl
O
se
for(i++;i<5;i++)
U
{
new_len=strlen(lines_arr[i]);
tre
/* Determines the length of the next element */
en
if(new_len>prev_len)
l_ctr=i;
C
/* Stores the subscript of the longer string */
h
ec
prev_len=new_len;
}
pt
rA
return l_ctr;
/* Returns the subscript of the longest string */
Fo
y
to Functions-4
nl
O
se
Sample output of the program
U
tre
Enter string 1: The
en
Enter string 2: Sigma