Session 10

| Strings Session 10 Objectives Explain string variables and constants Explain pointers to strings Perform string input/output operations Explain the various string functions Explain how arrays can be passed as arguments to functions Describe how strings can be used as function arguments String variables Strings are arrays of characters terminated by the NULL (‘\0’) character. String variables can be assigned string constants. A string constant is a sequence of characters surrounded by double quotes. The ‘\0’ null character is automatically added in the internal representation of a string. While declaring a string variable, allow one extra element space for the null terminator. Declaring string variables A typical string variable declaration is:. char str[10]; str is a character array variable that can hold a maximum of 10 characters including the null terminator. String I/O operations-1 String I/O operations are carried out using functions from the standard I/O library called The gets() function is the simplest method of accepting a string through standard input Input characters are accepted till the Enter key is pressed The gets() function replaces the terminating ‘\n’ new line character with the ‘\0’ character Syntax : gets(str); String I/O operations-2 The puts() function is used to display a string on the standard output device. Syntax : puts(str); The scanf() and printf() functions are used to accept and display mixed data types with a single statement. The syntax to accept a string is as follows: scanf(“%s”, str); The syntax to display a string is as follows: printf(“%s”, str); String Functions Functions for handling strings are found in the standard header file . Few of the operations performed by these functions are: Concatenating strings Comparing strings Locating a character in a string Copying one string to another Calculating the length of a string The strcat() function Joins two string values into one. Syntax: strcat(str1, str2); .

TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG