

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An excerpt from the 'intro to programming ii' course at the university of san francisco. It covers various aspects of handling strings as arrays of characters in c, including the use of string functions such as strlen, strcpy, strcat, and strcmp. The document also includes exercises to test the understanding of the concepts.
Typology: Study notes
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















27-0:^ Strings in C
Remember that C does not have a String data type. Instead, a string is just an array of characters. This means that we can access characters in a string justlike we do elements of an array.
Department of Computer Science โ University of San Francisco โ p.2/
27-2:^ String library
C has a number of useful functions built into the string.hlibrary. strlen strcpy/strncpy strcat/strncat strdup strcmp/strncmp
Department of Computer Science โ University of San Francisco โ p.4/
27-3:^ strlen
strlen takes a string as input and returns an intrepresenting the length of the string. int^ countXs(char
int^ i,^ count
for^ (i^ =^ 0;^
i^ <^ strlen(instr);
if^ ( instr[i]
==^ โxโ^ ||^
instr[i]^ ==^
Department of Computer Science โ University of San Francisco โ p.5/
27-5:^ strcpy
strcpy takes two arguments: dest and src. strcpy makes a copy of src in dest. char^ in[80]^
=^ โโhello^ worldโโ; char^ out[80];strcpy(out,in);printf(โโ%sโโ,
out);
Department of Computer Science โ University of San Francisco โ p.7/
27-6:^ Exercise
What if strcpy didnโt exist? How would we write it?
Department of Computer Science โ University of San Francisco โ p.8/
27-8:^ strcmp
strcmp(s1,s2) compares two strings, s1 and s2. Semantics are the same as compareTo.^ s1 < s2: return < 0.^ s1 == s2: return 0^ s1 > s2: return > 0.
Department of Computer Science โ University of San Francisco โ p.10/
27-9:^ Example
char^ *name1,
*name2; printf(โโenter
name^ 1:โโ); scanf(โโ%sโโ,name1);printf(โโenter
name^ 2:โโ); scanf(โโ%sโโ,name2);if^ (strcmp(name1,name2)
printf(โโname
1 comes^ first.โโ);
printf(โโname
2 comes^ first.โโ);
Department of Computer Science โ University of San Francisco โ p.11/
27-11:^ strcat
strcat(dest, src); strcat appends a copy of the characters in src to the stringdest. dest must have enough room for all of the characters insrc.
Department of Computer Science โ University of San Francisco โ p.13/
27-12:^ Example
char^ s1[80]^
=^ โโworld^ โโ; char^ s2[80]^
=^ โโhello^ โโ; strcat(s2,s1);printf(โ%sโโ,s2);
Department of Computer Science โ University of San Francisco โ p.14/
27-14:^ strdup
strdup returns a pointer to a copy of the input string. These pointers refer to different memory locations.
Department of Computer Science โ University of San Francisco โ p.16/
27-15:^ Example
char^ *s1^ =^
โโHello^ worldโโ; char^ *s2^ =^
strdup(s1); s2[5]^ = โQโ;printf(โโ%s^
%sโโ,^ s1,^ s2);
Department of Computer Science โ University of San Francisco โ p.17/
27-17:^ Bitwise operations
Letโs say we need to keep track of whether 16 computersare on or off. How should we represent this?
Department of Computer Science โ University of San Francisco โ p.19/
27-18:^ Bitwise operations
Letโs say we need to keep track of whether 16 computersare on or off. How should we represent this? Sixteen separate variables? ick. An array of 16 ints? Better, but wasteful. Letโs do it with a single int.
Department of Computer Science โ University of San Francisco โ p.20/