You are on page 1of 20

Fo

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

 Describe how strings can be used as function


pt
rA

arguments
Fo

Elementary Programming with C/Session 1/ 2 of 20


y
String variables

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

 The ‘\0’ null character is automatically added in the


pt

internal representation of a string.


rA

 While declaring a string variable, allow one extra


Fo

element space for the null terminator.


Elementary Programming with C/Session 1/ 3 of 20
y
Declaring string variables

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

of 10 characters including the null terminator.


pt
rA
Fo

Elementary Programming with C/Session 1/ 4 of 20


y
String I/O operations-1

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

Elementary Programming with C/Session 1/ 5 of 20


String I/O operations-2

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

 The syntax to accept a string is as follows:


pt

scanf(“%s”, str);
rA

 The syntax to display a string is as follows:


Fo

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

• Locating a character in a string


pt

• Copying one string to another


rA

• Calculating the length of a string


Fo

Elementary Programming with C/Session 1/ 7 of 20


y
The strcat() function

nl
O
se
 Joins two string values into one.

U
tre
 Syntax:

en
C
strcat(str1, str2);
h
ec

 Concatenates the str2 at the end of str1


pt
rA

 The function returns str1


Fo

Elementary Programming with C/Session 1/ 8 of 20


y
The strcmp() function

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

 The function returns a value:


pt

• Less than zero if str1<str2


rA

• Zero if str1 is same as str2


Fo

• Greater than zero if str1>str2


Elementary Programming with C/Session 1/ 9 of 20
y
The strchr() function

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

• Pointer to the first occurrence of the character


rA

(pointed by chr) in the string, str


• NULL if it is not present
Fo

Elementary Programming with C/Session 1/ 10 of 20


y
The strcpy() function

nl
O
se
U
 Copies the value in one string onto another

tre
en
 Syntax:
C
strcpy(str1, str2);
h
ec

 The value of str2 is copied onto str1


pt
rA

 The function returns str1


Fo

Elementary Programming with C/Session 1/ 11 of 20


y
The strlen() function

nl
O
se
U
 Determines the length of a string

tre
en
 Syntax:
C
strlen(str);
h
ec

 The function returns an integer value for the


pt

length of str
rA
Fo

Elementary Programming with C/Session 1/ 12 of 20


Passing Arrays to

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

for(ctr=0;ctr<5;ctr++) /* Accepts numbers into the array */


{
pt

printf("\nEnter number %d: ", ctr+1);


rA

scanf("%d", &num[ctr]);
}
Fo

Elementary Programming with C/Session 1/ 14 of 20


Passing Arrays to

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

for(i=0,total=0;i<5;i++) /* Calculates the sum */


rA

total+=num_arr[i];
Fo

return total; /* Returns the sum to main() */


}
Elementary Programming with C/Session 1/ 15 of 20
Passing Arrays to

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

The sum of the array is 75

Elementary Programming with C/Session 1/ 16 of 20


Example of Passing Strings

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

/* Accepts string values into the array */


{
Fo

printf("\nEnter string %d: ", ctr+1);


scanf("%s", lines[ctr]);
} Elementary Programming with C/Session 1/ 17 of 20
Example of Passing Strings

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

Elementary Programming with C/Session 1/ 18 of 20


Example of Passing Strings

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

Elementary Programming with C/Session 1/ 19 of 20


Example of Passing Strings

y
to Functions-4

nl
O
se
Sample output of the program

U
tre
Enter string 1: The

en
Enter string 2: Sigma

Enter string 3: Protocol C


h
ec

Enter string 4: Robert


pt

Enter string 5: Ludlum


rA
Fo

The longest string is Protocol

Elementary Programming with C/Session 1/ 20 of 20

You might also like