2-Strings (3)
2-Strings (3)
サルバド-ル・フロランテ 1
Q: What is a string?
• a list of characters from a given character set (we
will use ASCII)
• a string constant is list of characters enclosed
within a pair of double quotes
“ABC”
“DLSU”
“X”
“ “
“”
“Hello world!\n”
“http://www.abc_def_123.org”
2
String internal representation
“DLSU” represented as a list of characters
3
How are the following represented as a list of:
(a) characters, (b) ASCII values?
• “Hello world!”
• “1 + 3 = 4”
• “X”
• “ ”
• “” (this represents an empty string)
4
Q: How do you declare a string variable?
5
Q: How do you define a string?
A string can be defined either as:
char animal[4] = {‘D’,‘O’,‘G’,‘\0’};
or
Note: make sure that the array size includes the null
byte!
6
Write 5 string definitions for the following (use a
different variable name for each string):
• “Hello world!”
• “1 + 3 = 4”
• “X”
• “ ”
• “”
7
String Initialization
char animal[4];
animal[0] = ‘D’;
animal[1] = ‘O’;
animal[2] = ‘G’;
animal[3] = ‘\0’;
8
String Initialization (pointers)
char animal[4];
*(animal + 0) = ‘D’;
*(animal + 1) = ‘O’;
*(animal + 2) = ‘G’;
*(animal + 3) = ‘\0’;
9
Declare 5 string variables and initialize them
accordingly with the following values:
• “Hello world!”
• “1 + 3 = 4”
• “X”
• “ “
• “”
10
String I/O with scanf() and printf()
#include <stdio.h>
int main()
{
char string[30];
scanf(“%s”, string); // note: no &, no \n
printf(“%s\n”, string);
return 0;
}
11
String Library Functions
12
String Functions
Pre-defined library functions in string.h
13
14
size_t strlen(const char * _Str);
// strlen() example #1
#include <stdio.h>
#include <string.h>
int
main()
{
printf(“%d\n”, (int) strlen(“Hello world!”));
printf(“%d\n”, (int) strlen(“DLSU”));
printf(“%d\n”, (int) strlen(“X”));
printf(“%d\n”, (int) strlen(“ ”)); // blank space
printf(“%d\n”, (int) strlen(“”)); // empty string
return 0;
}
15
size_t strlen(const char * _Str);
// strlen() example #2
#include <stdio.h>
#include <string.h>
int
main()
{
char str[255] = “Is CCPROG2 easy or difficult?”;
int i, len;
scanf(“%s”, str);
len = strlen(str);
for (i = 0; i < len; i++)
printf(“str[%d] = %c\n”, i, str[i]);
return 0;
} 16
Exercise: Implement Count_UpperCase() which
will count and return the number of upper case letters
in a string.
Example:
• Count_UpperCase(“Hello world!”) returns 1
• Count_UpperCase(“Big Bad Wolf”) returns 3
• Count_UpperCase(“abc”) returns 0
• Count_UpperCase(“”) returns 0
17
char * strcpy(char * _Dest, const char * _Source);
// strcpy() example #1
#include <stdio.h>
#include <string.h>
int
main()
{
char string[255];
char temp[20] = “Goodbye”;
return 0;
} 18
// strcpy() example #2
#include <stdio.h>
#include <string.h>
int
main()
{
char string[255];
char super[20] = “SUPERMAN”;
return 0;
}
19
// strcpy() example #3
#include <stdio.h>
#include <string.h>
int
main()
{
char temp[5];
return 0;
}
20
char * strcat(char * _Dest, const char * _Source);
// strcat() example #1
#include <stdio.h>
#include <string.h>
int
main()
{
char string[255];
char temp[10] = “ world!”;
strcpy(string, “Hello”);
strcat(string, temp);
printf(“%s\n”, temp);
printf(“%s\n”, string);
return 0;
}
21
// strcat() example #2
#include <stdio.h>
#include <string.h>
int
main()
{
char string[255];
char temp[10] = “ world!”;
return 0;
}
22
// strcat() example #3
#include <stdio.h>
#include <string.h>
int
main()
{
char string[255];
char temp[10] = “ world!”;
return 0;
}
23
// strcat() example #4
#include <stdio.h>
#include <string.h>
int
main()
{
char string[255];
char temp[10] = “ world!”;
return 0;
}
24
int strcmp(const char * _Str1, const char * _Str2);
// strcmp() example
#include <stdio.h>
#include <string.h>
int
main()
{
char str1[8] = “CCPROG2”;
char str2[8] = “abc”;
char str3[8] = “ABC”;
return 0;
} 25
typedef
typedef <type name> <alias>;
Example:
typedef char string[255];
typedef char Str30[31];
string greetings;
Str30 word;
26
// typedef example
#include <stdio.h>
#include <string.h>
int
main()
{
Str7 subject1 = “CCPROG2”;
Str7 subject2 = “GRAPHIX”;
return 0;
}
27
1D Array of Strings
typedef char String[6];
#define MAX 5
typedef char Str30[31];
int
main()
{
Str30 S[MAX];
int i;
return 0;
}
30
// EXERCISE: 1D array of strings
#include <stdio.h>
#include <string.h>
#define MAX 5
typedef char Str30[31];
int
main()
{
Str30 S[MAX];
int i;
Input_StringArray( , MAX);
Print_StringArray( , MAX);
return 0;
}
31
2D Array of Strings
typedef char String30[31];
32
// example: 2D array of strings
#include <stdio.h>
#include <string.h>
#define ROW_SIZE 5
#define COL_SIZE 5
typedef char Str30[31];
int
main()
{
Str30 M[ROW_SIZE][COL_SIZE];
int i, j;
// other codes...
return 0;
} 33
Exercise: Solve the following problems (from our Course Notes)
34
35
36
37
-- The End --
サルバド-ル・フロランテ 38