0% found this document useful (0 votes)
8 views

2-Strings (3)

The document provides an overview of strings in programming, detailing their definition, internal representation, and how to declare and initialize string variables in C. It covers string operations using library functions such as strlen, strcpy, strcat, and strcmp, along with examples of their usage. Additionally, it discusses the use of typedef for creating string types and demonstrates working with 1D and 2D arrays of strings.

Uploaded by

White Listed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

2-Strings (3)

The document provides an overview of strings in programming, detailing their definition, internal representation, and how to declare and initialize string variables in C. It covers string operations using library functions such as strlen, strcpy, strcat, and strcmp, along with examples of their usage. Additionally, it discusses the use of typedef for creating string types and demonstrates working with 1D and 2D arrays of strings.

Uploaded by

White Listed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Strings

/* Read Chapter 2 of the Course Notes for details. */

サルバド-ル・フロランテ 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

“DLSU” as a list of ASCII values


68 76 83 85 0
A null byte terminates the string (end of string).

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?

• a string variable stores characters in


contiguous memory
• In CCPROG2, we’ll use 1D array of characters
to declare and store a string.
• Examples:
char animal[4];
char name[10];
char string[256];

5
Q: How do you define a string?
A string can be defined either as:
char animal[4] = {‘D’,‘O’,‘G’,‘\0’};

or

char animal[4] = “DOG”;

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’;

The following is INCORRECT!


animal = “DOG”; // There’s NO array-to-array assignment!

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

Pre-defined library functions in string.h


• strlen() // string length
• strcpy() // string copy
• strcat() // string concatenate
• strcmp() // string compare
• … etc …

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;

printf(“%d\n”, (int) strlen(str));

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”;

// string = “Hello world!”; // is erroneous!


strcpy(string, “Hello world!”); // thus strcpy() was born
printf(“%s\n”, string);

printf(“%s\n”, strcpy(string, temp));

printf(“%d\n”, (int) strlen( strcpy(string, “CCPROG2”)) );

return 0;
} 18
// strcpy() example #2
#include <stdio.h>
#include <string.h>
int
main()
{
char string[255];
char super[20] = “SUPERMAN”;

printf(“%s\n”, strcpy(string, super));


strcpy(string + 5, “GIRL”); // pointer arithmetic here
printf(“%s %s\n”, super, string);

return 0;
}

19
// strcpy() example #3
#include <stdio.h>
#include <string.h>
int
main()
{
char temp[5];

// Question: why is the following a BAD code?


strcpy(temp, “Hello world!”);

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!”;

strcat( strcpy(string, “Hello”), temp);


printf(“%s\n”, string);

return 0;
}

22
// strcat() example #3
#include <stdio.h>
#include <string.h>
int
main()
{
char string[255];
char temp[10] = “ world!”;

printf(“%s\n”, strcat( strcpy(string, “Hello”), temp));

return 0;
}

23
// strcat() example #4
#include <stdio.h>
#include <string.h>
int
main()
{
char string[255];
char temp[10] = “ world!”;

strcat( strcpy(string, “Hello”), temp );

// Question: why is the following a BAD code?


strcat(temp, string);

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”;

printf(“%d”, strcmp(“Hello”, “World”));


printf(“%d”, strcmp(“Hello”, str1));
printf(“%d”, strcmp(str1, str1));
printf(“%d”, strcmp(str2, str3));
printf(“%d”, strcmp(str3, str2));
printf(“%d”, strcmp(str3 + 2, str2 + 1);

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>

typedef char Str7[8];

int
main()
{
Str7 subject1 = “CCPROG2”;
Str7 subject2 = “GRAPHIX”;

// some other codes

return 0;
}

27
1D Array of Strings
typedef char String[6];

String S[5]; // 1D array of string

strcpy(S[0], “ANT”); ANT S[0]


strcpy(S[1], “CAT”);
strcpy(S[2], “DOG”); CAT S[1]
strcpy(S[3], “OWL”); DOG S[2]
strcpy(S[4], “ZEBRA”);
OWL S[3]
ZEBRA S[4]
28
ANT S[0]
CAT S[1]
DOG S[2]
OWL S[3]
ZEBRA S[4]

‘A’ ‘N’ ‘T’ ‘\0’


‘C’ ‘A’ ‘T’ ‘\0’
‘D’ ‘O’ ‘G’ ‘\0’
‘O’ ‘W’ ‘L’ ‘\0’
‘Z’ ‘E’ ‘B’ ‘R’ ‘A’ ‘\0’
29
// example: 1D array of strings
#include <stdio.h>
#include <string.h>

#define MAX 5
typedef char Str30[31];

int
main()
{
Str30 S[MAX];
int i;

for (i = 0; i < MAX; i++)


scanf(“%s”, S[i]); // note: no &

for (i = 0; i < MAX; i++)


printf(“%s\n”, S[i]);

return 0;
}
30
// EXERCISE: 1D array of strings
#include <stdio.h>
#include <string.h>

#define MAX 5
typedef char Str30[31];

// TASK #1: Implement Input_StringArray()


// TASK #2: Implement Print_StringArray()

int
main()
{
Str30 S[MAX];
int i;

Input_StringArray( , MAX);
Print_StringArray( , MAX);

return 0;
}
31
2D Array of Strings
typedef char String30[31];

String30 M[3][3]; // 2D array


ant owl dog
mouse ox cat
strcpy(M[0][0], “ant”);
horse fox bear
:
strcpy(M[1][1], “ox”);
:
strcpy(M[2][2], “bear”);

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;

// input 2D array elements


for (i = 0; i < ROW_SIZE; i++)
for (j = 0; j < COL_SIZE; j++)
scanf(“%s”, M[i][j]); // note: no &

// other codes...
return 0;
} 33
Exercise: Solve the following problems (from our Course Notes)

34
35
36
37
-- The End --

サルバド-ル・フロランテ 38

You might also like