Chapter 5b - Built in Functions
Chapter 5b - Built in Functions
Chapter 5b - Built in Functions
• Copying:
strcpy(array1,array2) or strncpy(array1,array2,num)
• Concatenation:
strcat(array1,array2) or strncat(array1,array2,num)
• Length
strlen(array1)
• Compare
strcmp(array1,array2)
More details on Char Array
Functions
Function Description
std::strcpy(string1, string2) Copies string2 into string1. (Unsafe)
std::strncpy(string1, string2, Copies string2 into string1 but limit the
length) number of characters copied (including the end
of string) to l e ngt h. (Safer)
std::strcat(string1, string2) Concatenates string2 onto the end of
string1. (Unsafe)
std::strncat(string1, string2, Concatenates string2 onto the end of
length) string1. Limit the number of characters
added to l e ngt h. Does not guarantee that
an end of string will be copied. ( Sa f e r )
length = std::strlen(string) Gets the length of a string. (Safe)
std::strcmp(string1, string2) 0 if string1 equals strings2,
otherwise non-zero. (Safe)
More details on Char Array
Functions
• Another C++ library, <cstring>, contains many functions
that are useful for handling C-strings
• strlen(string_exp) // length without null character
• Insert:
string1.inset(n1,string2) or string1.insert(n1,n2,’b’)
• Find:
string1.find(string2,num1) or string2.find(‘b’,num2)
• Concatenation: use the + sign
• Length
string1.length() or string1.size()
• Compare
string1.compare(string2) or string1.compare(n1,n2,string2)
• Clearing (emptying)
string1.clear()
• Substring
string1.substring(num1,num2)
Details on string functions
stringname.compare(str1) compares stringname to str1 similar to
strcmp(stringname, str1) for c-strings
stringname.compare(x, n, str1) compares stringname to str1 starting at position x
and comparing n characters.
stringname.substr(x, n) returns n characters of stringname beginning at
position x as a substring.
stringname.insert(x, str1) inserts str1 in strigname beginning at position x
stringname.insert(x, n, ‘d’) inserts n d’s in stringname starting at position x
stringname.find(str1, x) returns the first position at or beyond x where str1
is found.
stringname.find(‘s’, x) returns the first position at or beyond x where s is
found
str1.length( ); function can be used to determine the number of
characters in a string
str2.clear( ) function can be used to delete all the characters in
a string.
Examples Char Array
Functions
•String constants are enclosed in double quotes. Example: "Sam".
•The standard function std::strcpy can be used to copy a string.
•#include <iostream>
•#include <cstring>
•main(){
• char name[30]; // First name of someone
• strcpy(name, "Sam"); //dangerous
• cout<<"The name is ”<<name << '\n';
• return (0);
•}
•Note: #include <cstring> tells C++ that we are using the standard
string package.
Examples Char Array
Functions
•#include <cstring>
•#include <iostream>
•char first[100]; // first name
•char last[100]; // last name
•char full_name[100]; // full version of first and last name
•int main()
•{
• strcpy(first, "Steve"); // Initialize first name
• strcpy(last, "Oualline"); // Initialize last name
• strcpy(full_name, first); // full = "Steve"
• // Note: strcat not strcpy
• strcat(full_name, " "); // full = "Steve "
• strcat(full_name, last); // full = "Steve Oualline"
• cout << "The full name is " << full_name << '\n';
• return (0);
•}
•Outputs:
• The full name is Steve Oualline
Examples String Functions
•#include <string>
•#include <iostream>
•int main()
•{
• string line;
• cout << "Enter a line:";
• gets(line);
• cout << "The length of the line is: " <<
• line.length() << '\n';
• return 0;
•}
•When we run this program we get:
• Enter a line:test
• The length of the line is: 4
HOMEWORK – Part 1
1. Explain what the following would display given the two strings.
name1: "Billy" name2: "Sally"
a) strcpy(name1, name2) ______________________
b) strcat(name2, name1) ______________________
c) length=strlen(name1) _______________________
d) strcmp(name1, name2) ________________________
2. Accept a sentence from the user as a character array. Find all occurrences of the letter 'a' and replaced
them with a '*'. Display the new sentence
3. Accept a users first and last name as individual strings. Concatenate them into one variable with a space and
display the resulting full name.
4. a) Accept a word as either a string or char array from the user. Convert it to PigLatin by moving the first
letter to the end of the word and adding an “a” eg. pig -> igpa
b) Accept a string from the user and output as many stars as the length of the word
HelloWorld
**********
Homework – Part 2
5. Accept a sentence as a string from the user and count how
many vowels are in the sentence.