Sunteți pe pagina 1din 5

Question: 1

Assignment C++

Q.1: What is string? How they are initialized. Explain NULL terminated strings and illustrate
the purpose of following functions. a) b) c) d) strepy strcmp strlen strcat

Answer:

STRINGS
String is the collection/sequence of characters. In C++ we can work with strings by either using the character array or the predefined string class. In character data type only single of the memory is occupied while in string multiple numbers of bytes of memory are engaged. There are mainly two types of Strings which are commonly used in C++: C-strings and strings that are objects of the string class. We will first discuss first kind of Strings which are arrays of type char. We call these strings C-strings or C-Style strings because they were the only kind of strings available in the C language. Although strings created with the string class, have superseded C-strings in many situations, C-strings are still important for a variety of reasons. First, they are used in many C library functions. Second, they will continue to appear in legacy code for years to come and third C-strings are more primitive and therefore easier to understand on a fundamental level.

(i)

Character Array(C-String):

Character array is the simple array with the data type of char used to hold the strings of characters. To declare C-string variable, as traditionally, we use the data type in front of the C-string variable but the data type must be type char. Then in square bracket the size of the string is initialized. As for each character 1 byte of the memory is engaged but in strings special null character, represented by \0 is appended to the end of the strings to indicate the end of the string. Hence if a string has n characters then it requires an n+1 element array to store it. Thus the character a is stored in a single byte, whereas the single-character string a is stored in two consecutive bytes holding the character a and the null character, which is known as NULL terminated strings.

09CE37 1

Question: 1

Assignment C++

Initializing the C-string Variable


As other variables and arrays you can also initialize the C-string variable. There are two ways by which you can initialize the C-string variable either you place the string in the double quotation marks in with the equal to sine following the C-string variable or you can place every character of the string enclosed in single quotation marks in the curly braces following the C-string variable and equal to sign. char string[11]=DARYA MEMON; char string[11]={D,A,R,Y,A, , M,E,M,O,N}; Both types of declaration are legal.

(ii) Standard C++ String Class:


As previously we used to store the string in the arrays so it was very complicated, time consuming and stretching. So the standard C++ includes a string class which defines an easy way to store and access the strings. The string class also offers many advantages as you also combine two or more strings. The string class is safer convenient way to store the strings and work with them then the C-strings

Defining/ Initializing the C-string Objects


To define or assign the string object we place the string constant in the double quotation marks followed by the equal to sign after the string object. You can also define string object by placing the string constant in the parenthesis delimited by the double quotation marks. String s2=Darya Memon; Sting s3=(Darya Memon);

Both types of defining are legal.

09CE37 2

Question: 1

Assignment C++

NULL terminated Strings


In C++, a string is stored as a null-terminated char array. This means that after the last truly usable char there is a null, which is represented in C++ by '\0'. The subscripts used for the array start with zero (0). The following line declares a char array called str. C++ provides fifteen consecutive bytes of memory. Only the first fourteen bytes are usable for character storage, because one must be used for the string-terminating null. char str[15]; The following is a representation of what would be in RAM, if the string "Hello, world!" is stored in this array. Characters: Subscripts: H 0 e 1 l 2 l 3 o 4 , 5 6 w 7 o 8 r l d ! 9 10 11 12 13 14

a. strcpy( )
strcpy() is used to copy a null-terminated string into a variable. Given the following declarations, several things are posible. Syntax: strcpy(ptr1, ptr2); where ptr1 and ptr2 are pointers to char char S[25]; char D[25];

Putting text into a string: strcpy(S, "This is String 1."); Copying a whole string from S to D: strcpy(D, S); Copying the tail end of string S to D: strcpy(D, &S[8]);

If you fail to ensure that the source string is null-terminated, garbage value may occur.

09CE37 3

Question: 1

Assignment C++

b. strcmp( )
strcmp() is used to compare two strings. The strings are compared character by character starting at the characters pointed at by the two pointers. If the strings are identical, the integer value zero (0) is returned. As soon as a difference is found, the comparison is halted and if the point of difference in the first string is less than that in the second a negative value is returned; otherwise, a positive value is returned. Syntax: diff = strcmp(ptr1, ptr2); where diff is an integer and ptr1 and ptr2 are pointers to char

char s1[25] = "pat"; char s2[25] = "pet"; diff will have a negative value after the following statement is executed. diff = strcmp(s1, s2); diff will have a positive value after the following statement is executed. diff = strcmp(s2, s1); diff will have a value of zero (0) after the execution of the following statement, which compares s1 with itself. diff = strcmp(s1, s1);

c. strlen( )
strlen() returns the length of a string, excluding the null. The following code will result in len having the value 13. Syntax: len = strlen(ptr); where len is an integer and ptr is a pointer to char int len; char str[15]; strcpy(str, "Hello, world!"); len = strlen(str);
09CE37 4

Question: 1

Assignment C++

d. strcat( )
strcat() is used to link together a null-terminated string to end of another string variable. This is equivalent to pasting one string onto the end of another, overwriting the null terminator. There is only one common use for strcat(). Syntax: strcat(ptr1, ptr2); where ptr1 and ptr2 are pointers to char char S[25] = "world!"; char D[25] = "Hello, ";

linking together the whole string S onto D:


strcat(D, S);

09CE37 5

S-ar putea să vă placă și