You are on page 1of 49

CMSC 21

Fundamentals of programming

Kristine Bernadette P. Pelaez

Institute of Computer Science


University of the Philippines Los Baños
Strings
Strings

a sequence of characters
But what is a
character??
Characters in C

written using single quotes


Characters in C

written using single quotes

‘A’‘b’‘5’
Characters in C

written using single quotes


they are just numbers encoded
using the ASCII encoding
Characters in C
int main(){
int num = 97;
printf(“num as char is %c.\n”,num);

printf(“num+5 as char is %c.\n”,num+5);

return 0;
}
Characters in C

has a datatype of char


Character Datatype

characters in C can store


only one character
Character Datatype

characters in C can store


only one character

char letter =‘Bee’;


Character Datatype

characters in C can store


only one character

char letter =‘Bee’;


Built-in funtions
for characters
functions are in the
ctype.h header file
Built-in funtions
for characters
functions are in the
ctype.h header file

isalpha(char c); isdigit(char c); isalnum(char c);

islower(char c); tolower(char c); ispunct(char c);

isupper(char c); toupper(char c); isspace(char c);


Strings

a sequence of characters
enclosed by double quotes
Strings

a sequence of characters
enclosed by double quotes
“ano ang tulog?”
“4Ng h1r4P pH0wxSz ng 21”
“di pa din ako tapos sa exer :(”
Strings in C

an array of characters
terminated by the
null character ‘\0’
String Declaration

just declare an array of char


String Declaration

just declare an array of char


char name[50];
char address[100];
char input[30];
char message[256];
String Declaration

can store up to N-1 characters


String Declaration

can store up to N-1 characters


one element is reserved for the
null terminator (which should be
placed at the end of the string)
String Declaration

char input[30];

can store up to 30 characters


but can hold only 29 characters
of input since one is reserved
for the null character
String Initialization

the same with array of char


String Initialization

the same with array of char


char name[10] = {‘k’,‘e’,‘i’,‘\0’};
String Initialization

the same with array of char


char name[10] = {‘k’,‘e’,‘i’,‘\0’};

‘k’ ‘e’ ‘i’ ‘\0’

0 1 2 3 4 5 6 7 8 9
name
String Initialization

give the array a string literal


char name[10] = “katherine”;

‘k’ ‘a’ ‘t’ ‘h’ ‘e’ ‘r’ ‘i’ ‘n’ ‘e’ ‘\0’

0 1 2 3 4 5 6 7 8 9
name
String Initialization

give the array a string literal


char name[] = “ivy joy”;

‘i’ ‘v’ ‘y’ ‘ ’ ‘j’ ‘o’ ‘y’ ‘\0’

0 1 2 3 4 5 6 7
name
String Initialization

give a string literal to a pointer


char *name = “dja”;

‘d’ ‘j’ ‘a’ ‘\0’

name 0 1 2 3
Use a String in C
int main(){
char name[20];
char *prompt = “What is your name?”;
printf(“%s”,prompt);
scanf(“%s”,name);

printf(“Your name is %s.\n”,name);

return 0;
}
Built-in funtions
for strings
functions are in the
string.h header file
Built-in funtions
for strings
functions are in the
string.h header file

strcmp strcat strchr

strcpy strlen strtok

AND MANY MOREEEEE!


String Manipulations
int main(){
char name[20];
char prompt[] = “What is your name?”;
name = prompt; //will this work?

return 0;
}
String Manipulations
int main(){
char name[20];
char prompt[] = “What is your name?”;
name = prompt; //will this work?

return 0;
} name is a static array
static arrays decay to
become constant pointers
String Manipulations
int main(){
char name[20];
char prompt[] = “What is your name?”;
name = “Maria”; //will this work?

return 0;
}
String Manipulations
int main(){
char name[20];
char prompt[] = “What is your name?”;
name = “Maria”; //will this work?

return 0;
} name is a static array
static arrays decay to
become constant pointers
String Manipulations
int main(){
char name[20];
char prompt[] = “What is your name?”;
strcpy(name,“Maria”); //will this work?

return 0;
}
String Manipulations
int main(){
char name[20];
char prompt[] = “What is your name?”;
strcpy(name,“Maria”); //will this work?

return 0;
}
Valid!
String Manipulations
int main(){
char name[20];
char *prompt;
prompt = name; //will this work?

return 0;
}
String Manipulations
int main(){
char name[20];
char *prompt;
prompt = name; //will this work?

return 0;
} Yes.
Prompt will now point to
st
the 1 element of name.
String Manipulations
int main(){
char name[20];
char *prompt;
prompt = “Welcome!”; //will this work?

return 0;
}
String Manipulations
int main(){
char name[20];
char *prompt;
prompt = “Welcome!”; //will this work?

return 0;
} Yes.
Prompt will now point to the
st
1 element of the string literal.
String Manipulations
int main(){
char name[20];
char *prompt;
strcpy(prompt,“Welcome!”);
//will this work?

return 0;
}
String Manipulations
int main(){
char name[20];
char *prompt;
strcpy(prompt,“Welcome!”);
//will this work?
No.
return 0; Because prompt is not
}
pointing to any string
which strcpy will try
to overwrite.
String Manipulations
int main(){
char name[20];
char *prompt;
prompt = “Good day!”; //this is valid
strcpy(prompt,“Welcome!”);
//will this work?

return 0;
}
String Manipulations
int main(){
char name[20];
char *prompt;
prompt = “Good day!”; //this is valid
strcpy(prompt,“Welcome!”);
//will this work? No.
return 0; Though prompt is pointing to a
} string, it is pointing to a string literal.
A string literal’s elements cannot be
changed unless it is in an array.
String Manipulations
int main(){
char name[20], *name2 = “name”;
char *prompt = “What is your name?”;
printf(“%s”,prompt);
scanf(“%s”,name);
scanf(“%s”,name2); //will this work?

printf(“Your name is %s.\n”,name);

return 0;
}
Announcements
We will have A
quiz
next meeting
CMSC 21
Fundamentals of programming

Kristine Bernadette P. Pelaez

Institute of Computer Science


University of the Philippines Los Baños

You might also like