Term Paper On String C++
Term Paper On String C++
Term Paper On String C++
TERM PAPER OF
OBJECT ORIENTED CONCEPTS
On
Jalandhar-Delhi G.T. Road (NH-1), Phagwara, Punjab (INDIA) - 144402. TEL: +91-
1824-404404 Toll Free: 1800 102 4431 info@lpu.co.in
With regards I would like to thanks my Lect. M/s. …………… who helped me in
completing my Term Paper on the topic “DEFINE CLASS STRING AND
IMLEMENT ALL THE FUNCTIONS RELATED TO STRINGS”. Of subject
“OBJECT ORIENTED CONCEPTS”. Due to his proper guidance and under the
shower of his eternal knowledge I was able to complete my Term Paper
comfortably which might not be possible without his efforts.
I must say thanks to my friend who helped me in the completion of my Term
paper. I must say sorry for the errors if I have committed in my Term Paper.
Raj kumar
Date: ...............
a. char *strcpy( )
b. char *strncpy ( )
c. char *strcat( )
d. char *strncat( )
e. char *strchr( )
f. char *strrchr( )
g. strcmp( )
h. strspn( )
i. strcspn( )
j. strlen( )
k. strpbrk( )
m. char *strtok( )
V References
Introduction
Strings in C++ are represented by arrays of characters. The end of the string is marked with a
special character, the null character, which is simply the character with the value 0. Because C+
+ has no built-in facilities for manipulating entire arrays (copying them, comparing them, etc.), it
also has very few built-in facilities for manipulating strings.
In fact, C++'s only truly built-in string-handling is that it allows us to use string constants (also
called string literals) in our code. Whenever we write a string, enclosed in double quotes, C
automatically creates an array of characters for us, containing that string, terminated by the \0
character. For example, we can declare and define an array of characters, and initialize it with a
string constant:
To do anything else with strings, we must typically call functions. The C++ library contains a
few basic string manipulation functions, and to learn more about strings, we'll be looking at how
these functions might be implemented.
#include <string.h>
char string1[] = "Hello, world!";
char string2[20];
strcpy(string2, string1);
About strcopy
The destination string is strcpy's first argument, so that a call to strcpy mimics an assignment
expression (with the destination on the left-hand side). Notice that we had to allocate string2 big
enough to hold the string that would be copied to it. Also, at the top of any source file where
we're using the standard library's string-handling functions (such as strcpy) we must include the
line
#include <string.h>
which contains external declarations for these functions.
Abut stcmp
Since C won't let us compare entire arrays, either, we must call a function to do that, too. The
standard library's strcmp function compares two strings, and returns 0 if they are identical, or a
negative number if the first string is alphabetically ``less than'' the second string, or a positive
number if the first string is ``greater.'' (Roughly speaking, what it means for one string to be
``less than'' another is that it would come first in a dictionary or telephone book, although there
are a few anomalies.) Here is an example:
Another standard library function is strcat, which concatenates strings. It does not concatenate
two strings together and give you a third, new string; what it really does is append one string
onto the end of another. (If it gave you a new string, it would have to allocate memory for it
somewhere, and the standard library string functions generally never do that for you
automatically.) Here's an example:
If you have a string and you want to know its length, you can call strlen, which returns the length
of the string (i.e. the number of characters in it), not including the \0:
Finally, you can print strings out with printf using the %s format specifier, as we've been doing
in these examples already (e.g. printf("%s\n", string5);).
while(src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
}
We've called it mystrcpy instead of strcpy so that it won't clash with the version that's already in
the standard library. Its operation is simple: it looks at characters in the src string one at a time,
and as long as they're not \0, assigns them, one by one, to the corresponding positions in the dest
string. When it's done, it terminates the dest string by appending a \0. (After exiting the while
loop, i is guaranteed to have a value one greater than the subscript of the last character in src.)
For comparison, here's a way of writing the same code, using a for loop:
for(i = 0; src[i] != '\0'; i++)
dest[i] = src[i];
dest[i] = '\0';
Yet a third possibility is to move the test for the terminating \0 character out of the for loop
header and into the body of the loop, using an explicit if and break statement, so that we can
while(1)
{
if(str1[i] != str2[i])
return str1[i] - str2[i];
if(str1[i] == '\0' || str2[i] == '\0')
return 0;
i++;
}
}
Characters are compared one at a time. If two characters in one position differ, the strings are
different, and we are supposed to return a value less than zero if the first string (str1) is
alphabetically less than the second string. Since characters in C are represented by their numeric
character set values, and since most reasonable character sets assign values to characters in
alphabetical order, we can simply subtract the two differing characters from each other: the
expression str1[i] - str2[i] will yield a negative result if the i'th character of str1 is less than the
corresponding character in str2. (As it turns out, this will behave a bit strangely when comparing
upper- and lower-case letters, but it's the traditional approach, which the standard versions of
return i;
}
In this case, all we have to do is find the \0 that terminates the string, and it turns out that the
three control expressions of the for loop do all the work; there's nothing left to do in the body.
Therefore, we use an empty pair of braces {} as the loop body. Equivalently, we could use a null
statement, which is simply a semicolon:
for(i = 0; str[i] != '\0'; i++);
Empty loop bodies can be a bit startling at first, but they're not unheard of.
Everything we've looked at so far has come out of C's standard libraries. As one last example,
let's write a substr function, for extracting a substring out of a larger string. We might call it like
this:
As we may also have mentioned, there is a big difference between a character and a string, even
a string which contains only one character (other than the \0). For example, 'A' is not the same as
"A". To drive home this point, let's illustrate it with a few examples.
On the other hand, when you need a string, you must use a string. To print a single newline, you
could call
printf("\n");
It would not be correct to call
printf('\n'); /* WRONG */
printf always wants a string as its first argument. (As one final example, putchar wants a single
character, so putchar('\n') would be correct, and putchar("\n") would be incorrect.)
We must also remember the difference between strings and integers. If we treat the character '1'
as an integer, perhaps by saying
int i = '1';
we will probably not get the value 1 in i; we'll get the value of the character '1' in the machine's
character set.
Just as the character '1' is not the integer 1, the string "123" is not the integer 123. When we have
a string of digits, we can convert it to the corresponding integer by calling the standard function
atoi:
1. char *strcpy( )
copies the string s2 into the character array s1. The value of s1 is returned.
#include
#include
int main ()
{
char string1[]="test string";
char string2[80];
char string3[80];
char *string4;
strcpy (string2,string1);
strcpy (string3,"strcopy worked.");
string4 = strcpy(string2, "strcpy return example");
printf ("string1: %s\nstring2: %s\nstring3: %s\n",string1,string2,string3);
printf ("string4: %s\n",string4);
return 0;
}
2. char *strncpy ( )
copies at most n characters of the string s2 into the character array s1. The value of s1 is
returned.
The C++ strncpy function copies a specified number of characters from a source to a destination.
A null character is not appended to the destination, so it will only be null-terminated if the length
of the source string is less than the number of bytes being copied.
e.g
3. char *strcat( )
appends the string s2 to the end of character array s1. The first character from s2
overwrites the '\0' of s1. The value of s1 is returned.
The C++ strcat function is short for "string concatenate." Strcat appends a copy of a
source string to a destination string. The null terminator character in the destination will
be overwritten by the first character of the source and another null character will be
appended to the end of the resulting new string.
#include
#include
int main ()
{
char string1[80];
char string2[80];
char string3[80];
strcpy (string1,"This string ");
strcpy (string2,"Rose");
strcpy (string3,"s smell like old shoes.");
strcat (string1,"is concatenated.");
4. char *strncat( )
appends at most n characters of the string s2 to the end of character array s1. The first
character from s2 overwrites the '\0' of s1. The value of s1 is returned.
E.g
int main ()
{
char string1[80];
char string2[80];
char string3[80];
strcpy (string1,"This string ");
strcpy (string2,"Rose");
strcpy (string3,"s smell like old shoes.");
strncat (string1,"is concatenated. Extra stuff", 16);
strncat (string2,string3,40);
printf ("string1: %s\n",string1);
printf ("string2: %s\n",string2);
return 0;
}
returns a pointer to the first instance of c in s. Returns a NULL pointer if c is not
encountered in the string.
The C++ strchr function locates the first occurrence of a specified character in a source
string and returns a pointer to that location. This includes the null character so strchr also
can be used to locate the end of a string. Strchr returns a null pointer if the specified
character is not found.
E.g
int main ()
{
char string1[] = "This is a test string";
char * character_pointer;
printf ("Looking for the 's' character in \"%s\"...\n",string1);
/* initialize character_pointer to the location of the first 's' */
/* in string1 */
character_pointer=strchr(string1,'s');
while (character_pointer!=NULL)
{
/* convert the pointer location to the position within string1 */
/* so it can be displayed */
printf ("found at %d\n",character_pointer - string1 + 1);
/* increment the pointer and search for the next 's' */
character_pointer = strchr(character_pointer + 1,'s');
}
return 0;
}
6. char *strrchr( )
The C++ strrchr function searches a source string for the last occurrence of a specified
character and returns a pointer to that occurrence. If there are no matches in the source
string, strrchr returns a null pointer. The search includes the null terminating character, so
strrchr can also return a pointer to the end of a string
E.g
int main ()
{
char string1[] = "abcd2abc3def5";
char * character_pointer;
/* search for the location of the last occurrence of 'c' in string1 */
character_pointer = strrchr (string1, 'c');
/* convert the pointers to position within string1. */
/* Note the increment by one for a one-based positional notation */
printf ("Last occurrence of 'c' found at %d \n", character_pointer - string1 + 1);
return 0;
}
7. strcmp( )
compares the string s1 to the string s2. The function returns 0 if they are the same, a
number < 0 if s1 < s2, a number > 0 if s1 > s2.
The C++ strcmp function compares two strings. It returns 0 if equal each other and a
nonzero value that indicates which string is greater.
E.g
int main ()
{
char testAnswer[] = "John";
8. strncmp( )
compares up to n characters of the string s1 to the string s2. The function returns 0 if
they are the same, a number < 0 ifs1 < s2, a number > 0 if s1 > s2.
The C++ strncmp function compares a specified number of bytes between two strings. It
returns 0 if they are equal to each other and a nonzero value to indicate which string is
greater.
E.g
int main ()
{
char string1[][3] = { "archer" , "arrange" , "array" };
int n;
puts ("Looking for words beginning with arr...");
for (n=0 ; n<3 ; n++)
if (strncmp (string1[n],"arr",3) == 0)
printf ("found %s\n",string1[n]);
return 0;
}
returns the length of the longest substring of s1 that begins at the start of s1and consists
only of the characters found in s2.
The C++ strspn function determines the length of the maximum initial part of a source
string that consists only of characters in a specified list. If every character in the source
string appears in the list, strspn returns the length of the source string. If the first
character in the source string is not in the list, strspn returns zero.
E.g
int main ()
{
int i;
char string1[] = "123abc123";
char string2[] = "123456";
char string3[] = "a123456";
char char_list[] = "1234567890";
/* The first three characters of string1 are numbers, so strspn */
/* returns three. */
i = strspn (string1,char_list);
printf ("The length of the initial numbers for string1 is %d.\n",i);
/* string2 consists entirely of digits, so strspn returns the length of */
/* string2. Note how the scan stops at the null terminator for string2. */
i = strspn (string2,char_list);
printf ("The length of the initial numbers for string2 is %d.\n",i);
/* The first character of string3 is not a digit, so even though the */
/* rest of string3 consists of digits, strspn returns 0.*/
i = strspn (string3,char_list);
printf ("The length of the initial numbers for string3 is %d.\n",i);
return 0;
}
returns the length of the longest substring of s1 that begins at the start of s1and contains
none of the characters found in s2.
The C++ strcspn function searches a source string for the first occurrence of characters in
a list and returns the number of characters read. This can include the terminating null
character, so strcspn will return the length of the source string if it does not contain any of
the characters for which it searches.
E.g
int main ()
{
char string1[] = "abcd2";
char char_list[] = "1234567890";
int i;
/* Notice how strcspn returns the number of characters scanned */
/* before a match is found, in this case 4 */
i = strcspn (string1,char_list);
/* add 1 to the return value of strcspn to get the position */
/* of the first matching character */
printf ("The first number in string1 is at position %d.\n",i+1);
return 0;
11.strlen( )
determines the length of the string s. Returns the number of characters in the string
before the '\0'.
The strlen function in C++ returns the length of the given string. This length is the
number of characters before the terminating null character
12.strpbrk( )
returns a pointer to the first instance in s1 of any character found in s2. Returns a NULL
pointer if no characters from s2 are encountered in s1.
The C++ strpbrk function searches a source string for the first occurrence of characters in
a list and returns a pointer to that first occurrence. If there are no matches in the source
string, strpbrk returns a null pointer. The search does not include the null terminating
character.
E.g
int main ()
{
char string1[] = "abcd2abc3def5";
char char_list[] = "1234567890";
char * character_pointer;
printf ("numbers in '%s': ",string1);
/* get the pointer to the first digit in string1 */
character_pointer = strpbrk (string1, char_list);
while (character_pointer != NULL)
{
/* display the digit we found */
13.strstr( )
returns a pointer to the first instance of string s2 in s1. Returns a NULL pointer if s2 is
not encountered in s1.
The C++ strstr function locates a specified substring within a source string. The scan does
not include terminating null-characters. Strstr returns a pointer to the first occurrence of
the substring in the source. If the substring is not found, strstr returns a null pointer. If the
substring has zero length, strstr returns the source string.
E.g
int main ()
{
char string1[] ="This is a test string";
char * character_pointer;
/* locate "test" within string1 using character_pointer */
character_pointer = strstr (string1,"test");
/* change the first four characters of the string */
/* pointed to by character_pointer to "long" */
strncpy (character_pointer,"long",4);
puts (string1);
/* Note how an empty substring returns a pointer to the */
14.char *strtok( )
repeated calls to this function break string s1 into "tokens"--that is the string is broken
into substrings, each terminating with a '\0', where the '\0' replaces any characters
contained in string s2. The first call uses the string to be tokenized as s1; subsequent calls
use NULL as the first argument. A pointer to the beginning of the current token is
returned; NULL is returned if there are no more tokens.
The C++ strtok function tokenizes a string. It splits a source string into segments called
tokens that are separated by any of a specified set of characters called delimiters. Strtok
takes the source string as an argument on its first call to scan for delimiters. It then uses
the position after the end of the last token to scan for the next token in subsequent calls.
E.g
int main ()
{
char string1[] ="- This, is a test string.";
char * character_pointer;
printf ("Splitting string \"%s\" into tokens:\n",string1);
character_pointer = strtok (string1," ,.-");
while (character_pointer != NULL)
{
/* Note that the delimiters (space, comma, period and hyphen) */
References
[1] http://www.cplusplus.com/reference/string/string/
[2] http://www.cplusplus.com/reference/string/string/string/
[3] http://www.cplusplus.com/reference/clibrary/cstring/
[4] http://anaturb.net/C/string_exapm.htm
[5] http://www.cplusplus.com/reference/clibrary/cstring/
[6] http://www.bgsu.edu/departments/compsci/docs/string.html
[7] http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: