CS 24000 Study Guide - Midterm Guide: Substring, C Dynamic Memory Allocation, Memory Leak

20 views2 pages

For unlimited access to Study Guides, a Grade+ subscription is required.

CS240&Lecture&4&Notes&review&1&&
String'functions'in'C'
C&provides&string&fucntions&such&as:&
o Stry(char&*dest,&char&*src)&
§ Copy&string&src&to&dest&
o Int&strlen(char&*s)&
§ Return&the&length&of&a&string&
o Char&*strcat(char&*dest,&char&*src)&
§ Concatenates&string&src&after&string&dest.&dest&should&point&to&a&
string&large&enough&to&have&both&dest&and&src&and&the&null&
terminator&
o Int&strcmp(char&*a,&char&*b)&
§ Compares&to&strings&a,&b.&returns&>0&is&a&is&larger&alphabetically&
that&b,&<0&if&b&is&larger&than&a,&or&0&if&a&and&b&are&equal&
&
String'functions'in'C'
Char&*strstr(const&char&*haystack,&const&char&*needle);&
o Find&the&first&occurrence&of&the&substring&needle&in&the&string&
haystack,&returning&a&pointer&to&the&fund&substring&
§ Char&strdup(const'chars);&
Return&a&duplicate&of&the&string&s&in&memory&allocated&
using&malloc&
§ Char&*strncpy(char&*dest,&const&char&*src,&size_t&n);&
Copy&at&most&n&bytes&from&string&src&to&dest,&returning&a&
pointer&to&the&start&of&dest&
&
Implementation'of'strlen'
char * strlen(char * src) {
int i = 0;
while (src[i] != ‘\0’) {
i++;
}
return i; }
&
Implementation'of'strcpy'using'array'operator'
char * strcpy(char * dest, char * src) {
int i = 0;
while (src[i] != ‘\0’) {
dest[i] = src[i];
// Copy character by char from src to dest. i++;
}!dest[i] =‘\0’; // Copy null terminator return dest;
}
IMPORTANT:&dest&should&point&to&a&block&of&memory&large&enough&to&store&
the&string&pointed&by&dest.&&
&
Allocating'an'array'using'malloc'
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+
$10 USD/m
Billed $120 USD annually
Homework Help
Class Notes
Textbook Notes
40 Verified Answers
Study Guides
1 Booster Class