Documente Academic
Documente Profesional
Documente Cultură
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++
09CE37 2
Question: 1
Assignment C++
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, ";
09CE37 5