int main (void) { printf( “%c”, “HELLO”[1]); return 0; } E [0] [1] [2] [3] [4] [5] H E L L O \0"> int main (void) { printf( “%c”, “HELLO”[1]); return 0; } E [0] [1] [2] [3] [4] [5] H E L L O \0">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings String - a string is a series of characters treated as a unit. A C string is a variable-length array of characters that is delimited by the null.

Similar presentations


Presentation on theme: "Strings String - a string is a series of characters treated as a unit. A C string is a variable-length array of characters that is delimited by the null."— Presentation transcript:

1 Strings String - a string is a series of characters treated as a unit. A C string is a variable-length array of characters that is delimited by the null character ( \0 ). The difference between a character stored in memory and a one character string stored in memory is that the string requires two memory location; one for the character and one for the null character. G G \0 \0 Character ‘G’ (one byte) String “G” (two bytes) "" empty string Storing Strings - A string is stored in an array of characters. It is terminated by a null character (‘\0’). The reason a string is terminated by a null character is because a string is a variable-length structure. The null character identifies the logical end of the structure within the array. The array that stores a string can be greater than or equal to the size of the string plus one for the null character.

2 #include <stdio.h>
Strings String Literal - A string literal - or as it is know, string constant - is a sequence of characters enclosed in double quotes. "Cleveland” When string literals are used in a program C automatically creates an array of characters, initializes it to a null delimited string, and stores it, remembering its address. It does all this because we use the double quotes that immediately identify the data as a string value. The literal, since it is an array of characters, is itself a pointer constant to the first element of the string. Generally, when you use it, you are referring to the entire string. #include <stdio.h> int main (void) { printf( “%c”, “HELLO”[1]); return 0; } E [0] [1] [2] [3] [4] [5] H E L L O \0

3 Strings A character array can be initialized with a string constant in conjunction with its declaration. char city[12] = “Cleveland” ; terminates with null [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] C l e v e l a n d \0 ? ? Since a string is stored in an array of characters, we do not need to indicate the size of the array if we initialize it when it is defined. char city[ ] = “Cleveland” ; [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] C l e v e l a n d \0 Another way to initialize a string is to declare a character pointer and initialize it with a string constant. char *city = “Cleveland” ; You could also initialize a string with character constants, but you must make sure your string is terminated with a null character. char city[10] = {‘C’, ’l’, ’e’, ’v’, ’e’, ’l’, ’a’, ’n’, ’d’, ’\0’}; As you can see this method is too tedious.

4 Strings String input/output functions - There are two basic ways to read and write strings in C; input/output function like scanf and printf or special set of string functions like gets and puts. scanf - The conversion code for a string is s. The scanf function first skips any leading whitespace character second, once it finds a character, it reads until it finds whitespace, putting each character in the array in order. Third, when it finds a trailing whitespace character it ends the string with a null character. Fourth, the whitespace character is left in the input stream. For example to read a string city from the keyboard, use the statement below: scanf( “%s”, city ); An address of operator is not required for city since it is already a pointer constant (name of the array). You have to make sure that the array is large enough to store all the data. If it isn’t, then you will destroy whatever follows the array in memory. You can protect against the user entering too much data by using a width in the field specification. The modified scanf statement is shown below scanf(“%9s”, city ); Another conversion code for strings is the Edit Set Conversion code ( %[…] ). The characters in the edit set identify the valid characters, known as the scanset, that are to be allowed in the string. All characters except the close bracket ( ‘]’ )can be included in the set.

5 Strings Each character read by scanf is compared against the edit set. If the character just read is in the edit set, it is placed in the string and the scan continues . The first character that does not match the scan set stops the read. The nonmatching character remains in the input stream for the next read operation. If the first character read is not in the edit set , the scanf terminates and a null string is returned. The read will stop if an end-of -file is detected or if the field width specification is included and the maximum number of characters has been read. Example : scanf(“%10[ ,-$]” , str) ; The above would read strings that contain only digits, commas, periods, the minus sign, and the dollar sign printf - To format string output use the conversion code %s with the printf function. printf(“%s”, city ); If city did not contain a null character (‘\0’) , run-time errors could result. The width can be specified with the %s as an option. If the string is smaller than the width then the string is printed right-justified with the space provided. The justification flag ( - ) is used to left justify the output. printf(“|%20s|\n”, “Cleveland” ); printf(“|%-20s|\n”, “Cleveland” ); output: | Cleveland| |Cleveland |

6 Strings gets and fgets - these functions take a line (terminated by a new line character from the input stream and make a null-terminated string out of it. The prototypes are: char *gets ( char *strPtr ) ; char *fgets ( char *strPtr, int size, FILE *fp ); The gets function converts the return (newline character) to the null character (\0) while the fgets puts it in the string and appends a null character. Both functions accept a string pointer and return the same if the input is successful. If there are any problems they return NULL. Since there is no length checking with gets it is better to use fgets. You can read from the keyboard with fgets by using stdin file pointer. kept the ‘\n and terminated with ‘\0’ From a file [0] [1] [2] [3] [4] [5] [6] fgets ( … ) H E L L O \n \0 changed ‘\n to a ‘\0’ since no size is specified , gets reads until EOF or ‘\n’ From the keyboard [0] [1] [2] [3] [4] [5] gets ( … ) H E L L O \0

7 (unless in string data)
Strings puts and fputs - these functions take a null-terminated string from memory and write it to the monitor or a file as a line. The prototypes are: int puts ( const char *strPtr ) ; int fputs (const char *strPtr, FILE *fp ); both functions change the string to a line. The null character is replaced with a newline in puts; it is dropped in fputs. It is the programmer’s responsibility to make sure the newline character is present at the appropriate places. If the write is successful, it returns a non-negative integer; if there are any transmission errors, it returns EOF. Note that the absence of a null character to terminate the string is not an error; however, it will most likely cause your program to fail. HELLO No newline (unless in string data) HELLO ‘\0’ is dropped To a file [0] [1] [2] [3] [4] [6] HELLO\n fputs ( … ) H E L L O \0 ‘\0’ is changed to ‘\n’ To the monitor [0] [1] [2] [3] [4] [5] puts ( … ) H E L L O \0

8 Example- Print to file and add a left margin
Strings Example- Print to file and add a left margin #include <stdio.h> int main (void) { FILE *outFile ; char strng[ 81 ] ; if( ! (outFile = fopen( “lines.txt”, “w” ) ) ) printf(“\could not open output file.\n ); exit(100) ; } while ( fgets(strng, sizeof( strng) - 1, stdin ) ) fputc(‘ ‘, outFile); fputs(strng, outFile ); if(strng[ sizeof(strng) - 1 ] != ‘\n’ ) fputs( “\n”, outFile ); fclose ( outFile ); return 0 ;

9 Strings Arrays of Strings - You can create an array of strings using a two-dimensional array of characters in which each row is one string. char seasons[4] [7] = {“winter”, “spring”, “summer”, “fall”}; [0] [1] [2] [3] [4] [5] [6] [0] w i n t e r \0 [1] s p r i n g \0 [2] s u m m e r \0 [3] f a l l \0 ? ? /* to print the seasons out */ for(i = 0 ; i < 4 ; i++) printf(“ %s \n”, seasons[ i ] ); /* or another way would be */ for(j = 0; j < 4; j++) printf("%s\n", *(seasons + j)); /* still another way would be /* { for(i = 0; i < 7; i++) printf("%c", *(*(seasons+j) + i)); printf("\n"); }

10 Strings Arrays of Strings(continued) - You can declare an array of pointers to character arrays. Each element of the array holds an address to a character array. The character arrays are of variable length. #include <stdio.h> int main (void) { char *pSeasons[4]; char **pLast; char **pWalker; pSeasons[0] = “winter”; pSeasons[1] = “spring”; pSeasons[2] = “summer”; pSeasons[3] = “fall”; printf(“\nThe seasons of the year are\n”); pLast = pSeasons + 3; for ( pWalker = pSeasons; pWalker <= pLast ; pWalker++) printf(“%s\n”, *pWalker); return 0 ; }

11 Strings String Manipulation Functions - Since a string is not a standard type, you cannot use it directly with most C operators. For example to move one string to another you must move the individual elements of the sending string to the receiving string. You cannot simply assign one string to another. C has provided a rich set of string functions. All the string functions, which are found in the string library ( <string.h>), have the prefix “str”. strlen - (string length) the strlen function returns the length of a string, specified as the number of characters in the string excluding the null character. If the string is empty , it returns zero. The prototype is as follows: int strlen ( const char *string ); strcpy - (string copy) , copies the contents of one string (including the null character) to another string. The address of the destination string is returned. The prototype is as follows: char *strcpy ( char *destination, const char *source); If the source string is longer than the destination string, the data in memory after the destination string are destroyed. It is the programmer’s responsibility to ensure that the destination string array is large enough to hold the sending string.

12 Strings Examples of strcpy: ? ? ? ? ? ? O h i o \0 ? s1 - before
strcpy ( s1, s2 ) O h i o \0 ? O h i o \0 ? s1 - after s2 - after I d a h o \0 C a t \0 ? ? s1 - before s2 - before strcpy ( s1, s2 ) C a t \0 o \0 C a t \0 ? ? s1 - after s2 - after

13 strncpy ( s1, s2, sizeof( s1 ) )
Strings strncpy - (string number copy) the strncpy function contains a parameter that specifies the maximum number of characters that can be moved at a time. The prototype is as follows: char *strcpy ( char *destination, const char *source, int size); In this function, size specifies the maximum number of characters that can be moved. If the source string is smaller than size, the entire string is copied and then null characters are placed in the destination string until exactly size characters have been filled. If the sending string is longer than size, the copy stops after size bytes have been copied. In this case the destination string variable will not be a valid string - it will not have a ‘\0’. If size is zero or negative nothing will be copied. ? ? ? ? ? ? C a t \0 ? ? s1 - before s2 - before strncpy ( s1, s2, sizeof( s1 ) ) C a t \0 \0 \0 C a t \0 ? ? s1 - after s2 - after I d a h o \0 S t a t e \0 s1 - before s2 - before strncpy ( s1, s2, 2 ) S t a h o \0 S t a t e \0 s1 - after s2 - after

14 int strncmp ( const char *string1, const char * string2, int size);
Strings strcmp - (string compare) the strcmp function compares two strings until unequal characters are found or until the end of the strings is reached. The prototype is as follows: int strcmp ( const char *string1, const char * string2); strncmp - (string number compare) the strncmp function compares two strings until unequal characters are found , a specified number of characters have been tested, or until the end of the strings is reached. The prototype is as follows: int strncmp ( const char *string1, const char * string2, int size); Both functions return an integer to indicate the results of the compare. If the two strings are equal, the return value is zero. If the first parameter is less than the second parameter, the return value is less than zero. A string, s1, is less than another string, s2, if when comparing character by character the s1 character is less than the s2 character, or the end of s1 is reached and s2 is not at its end. If the first parameter is greater than the second parameter, the return value is greater than zero. A string, s1, is greater than another string, s2, if when comparing character by character the s1 character is greater than the s2 character, or s1 is not at its end and the end of s2 is reached.

15 ‘D’ > ‘C’ therefore s1 is greater than s2
Strings D o g \0 ? ? C a t \0 ? ? s1 s2 strcmp ( s1, s2) ‘D’ > ‘C’ therefore s1 is greater than s2 D o g i e \0 D o g \0 ? ? s1 s2 strcmp ( s1, s2) ‘e’ > ‘\0’ therefore s1 is greater than s2 D o g i e \0 D o g \0 ? ? s1 s2 strncmp ( s1, s2, 3) ‘g’ == ‘g’ therefore s1 is equal to s2

16 Strings strcat and strncat - (string concatenate) the strcat function append one string to the end of a second string. Both functions return the address pointer to the destination string. The size of the destination string array is assumed to be large enough to hold the resulting string. If it isn’t the data at the end of the string array will be destroyed. The prototypes are as follows: char * strcat (char *string1, const char * string2); char * strcat (char *string1, const char * string2, int size); String2 is copied to the end of string 1 beginning with string1’s delimiter (\0). That is, the delimiter is replaced with the first character of string2. The delimiter from string 2 is copied to the resulting string to ensure that a valid string results. With strncat, if the length of string2 is less than size, then the call works the same as the basic string concatenation described above. However, if the length of string2 is greater than size, then only the number of characters specified by size are copied and a null character is appended at the end. D o g \0 ? ? ? C a t \0 ? s1- before s2 - before strcat ( s1, s2) D o g C a t \0 C a t \0 ? s1- after s2 - after

17 Strings strchr and strrchr - (string character) the strchr function searches for the first occurrence of a given character from the beginning of the string . The strrchr, searches for the first occurrence beginning at the end and working toward the beginning. The prototypes are as follows: char * strchr (const char *string, int ch); char * strrchr (const char *string, int ch); In either case , if the character is located, the function returns a pointer to it. If the character is not in the string, the function returns a null pointer. strstr - (string in string) the strstr function locates a string in a string starting from the beginning of the string. The prototypes are as follows: char *strstr (const char *string, const char *sub_string); strspn - (string span) the strspn searches the string, spanning characters that are in the set and stopping at the first character that is not in the set. It returns the number of characters that matched those in the set. If no characters match those in the set, it returns zero. The prototype is as follows: int strspn( const char *string1, const char *set ); strcspn - (string complement span ) the strcspn stops at the first character that matches one of the characters in the set. If all the characters in the string match the set, it returns the length of the string. The prototype is as follows:

18 Strings int strcspn( cnst char *string1, const char *set );
strtok - (string token) the strtok function is used to locate substrings, called tokens , in a string. Its most common use is to parse a string into tokens, much as a compiler parses lines of code. . The prototypes are as follows: char *strtok (const char *string, const char *delimiters); The first parameter is the string that is being parsed; the second parameter is a set of delimiters that are to be used to parse the first string. It first skips over all leading delimiter characters, if all the characters in the string are delimiters, then it terminates and returns a null pointer. When it finds a nondelimiter character, it changes its search and skips over all characters that are not in the set; that is, it searches until it finds a delimiter. When a delimiter is found, it is changed to a null character (‘\0’), which turns the token just parsed into a string.


Download ppt "Strings String - a string is a series of characters treated as a unit. A C string is a variable-length array of characters that is delimited by the null."

Similar presentations


Ads by Google