C Programming: Manipulating Strings in C, Study notes of Computer Science

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

Pre 2010

Uploaded on 07/30/2009

koofers-user-2tu
koofers-user-2tu ๐Ÿ‡บ๐Ÿ‡ธ

9 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Intro to Programming II
Strings in C
Chris Brooks
Department of Computer Science
University of San Francisco
Department of Computer Science โ€” University of San Francisco โ€“ p.1/??
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download C Programming: Manipulating Strings in C and more Study notes Computer Science in PDF only on Docsity!

Intro to Programming II^ Strings in C

Chris BrooksDepartment of Computer ScienceUniversity of San Francisco^ Department of Computer Science โ€” University of San Francisco โ€“ p.1/

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

*instr)^ {

int^ i,^ count

=^ 0;

for^ (i^ =^ 0;^

i^ <^ strlen(instr);

i++)^ {

if^ ( instr[i]

==^ โ€™xโ€™^ ||^

instr[i]^ ==^

โ€™Xโ€™)^ {

count++; } } }

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)

<^ 0)^ {

printf(โ€˜โ€˜name

1 comes^ first.โ€™โ€™);

}^ else^ if^ (strcmp(name1,name2)

> 0)^ {

printf(โ€˜โ€˜name

2 comes^ first.โ€™โ€™);

}^ else^ {^ printf(โ€˜โ€˜equal.โ€™โ€™); }

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/