Vous êtes sur la page 1sur 23

Strings

Ahmed Ali Qureshi

 

Strings are arrays of characters. The special character ' \0' (NUL) is used to indicate the end of a string. The line #include <string.h> is needed to inform C++ that you are using the string function library.

Example


This creates a character array four elements long. Note that we had to allocate one character for the end-of-string marker.

String constants consist of text enclosed in double quotes (").

C++ does not allow one array to be assigned to another, so you can't write an assignment of the form: name = "Sam"; // Illegal

Instead you must use the standard library function strcpy to copy the string constant into the variable. (strcpy copies the whole string including the end-of string character.)

To initialize the variable name to "Sam" you would write:

C++ uses variable-length strings.

This creates an array (string) that can contain up to 50 characters. The size of the array is 50, but the length of the string is 3. Any string up to 49 characters long can be stored in string. (One character is reserved for the NULL that indicates the end of the string.)

strlen() & sizeof()

String Routines

Example

Example

Reading Strings


cin.getline (string, sizeof(string));

When reading a string, the cin class considers anything up to the end-of-line part of the string.

Initializing the Strings




To initialize the variable name to the string "Sam" we use the statement:

C++ has a special shorthand for initializing strings, by using double quotes ('') to simplify the initialization. The previous example could have been written:

In previous example (on last slide)




The dimension of name is 4, because C++ allocates a place for the '\0' character that ends the string.

Example


Consider the following declaration

 

This creates an array (string) that can contain up to 50 characters. The size of the array is 50, and the length of the string is 3. Any string up to 49 characters long can be stored in string. (One character is reserved for the NUL that indicates the end of the string.) The statement initialized only 4 of the 50 values in string. The other 46 elements are not initialized and may contain random data.

More About Strings


 

   

strcat(s1 strcat(s1, s2) - It concatenates two strings. It strings. concatenates s2 at the end of s1. strcmp (s1, s2) It is used to compare strings. It (s1 strings. returns 0 if they are equal and less than 0 if s1<s2 and <s2 greater than 0 if s1>s2. >s2 strlen(s1 strlen(s1) It returns the length of the string s1. strcpy(s1, s2) - It copies s2 into s1. strcpy(s1 strncat(s1 strncat(s1, s2, n) - It appends substring to the string. It string. takes first n characters of string s2 and appends s1.

strncmp(s1,s2,n) strncmp(s1,s2,n) It compares first n characters of string s1 with first n characters of string s2. strchr(s1 strchr(s1, ch) Finds character ch in string s1. It returns the pointer at the first occurrence of ch. ch. strstr(s1 strstr(s1, s2) Finds substring s2 in s1. It returns a pointer at the first occurrence of s1.

Question 1


Write a function that returns the number of occurrences of a given character wthin a given string. string.

Question 2


Write a program that reads one line of text and then prints it with all its letters capitalized.

A Note About Output




This does not give a very precise control over input. C++ does a reasonable job for simple input. If the program expects a number and user types <enter> instead, the program will skip the <enter> (it's white space) and wait for user to type a number.

Vous aimerez peut-être aussi