Professional Documents
Culture Documents
13
Introduction to Programming
Strings
Introduction to Programming
Implicit Strings
W e l c o m e \n \0
Introduction to Programming
Implicit Strings
• Why implicit?
The program does not have any control over
the size or number of elements in the array,
which was implicitly sized by the compiler.
Introduction to Programming
Variable/Explicit Strings
C a o \0
Introduction to Programming
Examples
Introduction to Programming
Examples
char other[100] = “Maria Makiling”;
– specifying a larger size leaves space in the
array for other characters to be appended
– compiler still appends '\0' after the implicit
string
int i;
char cool[] = “Programming”;
while(cool[i] != ‘\0’)
printf(“%c”, cool[i++]);
Introduction to Programming
Printing Strings
Introduction to Programming
Accepting/Reading Strings
• Using scanf()
scanf( “%s”, cool );
Introduction to Programming
Accepting/Reading Strings
• Same as
scanf( “%s”, &cool[0] );
Introduction to Programming
Example – gets()
...
puts(“Anong pangalan mo? ”);
gets( string );
printf(“Ikaw si ”);
puts( string);
...
Introduction to Programming
Assigning to String
• Valid (initialization)
– char who[] = “Ser Wilmarc”;
• Invalid
– who = “Mom Wilmarc”;
Introduction to Programming
Assigning to String
#include <stdio.h>
#include <string.h>
main(void)
{
char who[20];
strcpy(who, “Manong Fishball”);
strcpy(who, “Tuition Fee Increase”);
printf(“%s”,who);
return 0;
}
Introduction to Programming
String Handling Functions in string.h
char s1[50] = “UP”;
char s2[50] = “UP System”;
Introduction to Programming
String Handling Functions in string.h
Introduction to Programming
String Handling Functions in string.h
strcpy(s1,“Hello”);
strcat(s1, “ world!”);
printf(“%s”,s1);
Introduction to Programming
String Handling Functions in string.h
Introduction to Programming