Professional Documents
Culture Documents
10 Strings PDF
10 Strings PDF
Fundamentals of programming
a sequence of characters
But what is a
character??
Characters in C
‘A’‘b’‘5’
Characters in C
return 0;
}
Characters in C
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
char input[30];
0 1 2 3 4 5 6 7 8 9
name
String Initialization
‘k’ ‘a’ ‘t’ ‘h’ ‘e’ ‘r’ ‘i’ ‘n’ ‘e’ ‘\0’
0 1 2 3 4 5 6 7 8 9
name
String Initialization
0 1 2 3 4 5 6 7
name
String Initialization
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);
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
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?
return 0;
}
Announcements
We will have A
quiz
next meeting
CMSC 21
Fundamentals of programming