Slot 16 17 18 Strings
Slot 16 17 18 Strings
Objectives
After studying this section, you should be able to:
The way is used to store a string of characters in C.
How to declare/initialize a string in C?
How to access a character in a string?
What are operations on strings
• Input/output (stdio.h)
• Some common used functions in the library string.h
How to manage an array of strings?
25/02/2025 2
Programming Fundamentals using C
Contents
Null-String/C-String
Declare/Initialize a string
Data stored in a string
Output a String
Input a string
May Operators Applied to String?
Other String Functions
Array of Strings
25/02/2025 3
Programming Fundamentals using C
1. Null-String/ C-String
A string is a group of characters. It is similar to an array of characters.
A NULL byte (value of 0 - escape sequence ‘\0’) is inserted to the end of a
string. It is called NULL-string or C-string.
A string is similar to an array of characters. The difference between them is at
the end of a string, a NULL byte is inserted to locate the last meaningful
element in a string.
If a string with the length n is needed, declare it with the length n+1.
25/02/2025 4
Programming Fundamentals using C
25/02/2025 5
Programming Fundamentals using C
25/02/2025 6
Programming Fundamentals using C
Note: Using malloc(…) and calloc(…) funtions in <stdlib.h>
25/02/2025 7
Programming Fundamentals using C
25/02/2025 8
Programming Fundamentals using C
25/02/2025 9
Programming Fundamentals using C
4. Output Strings
Formatted Output:
printf() library functions support the %s conversion specifier for character string
output
printf() displays all of the characters from the address provided up to but
excluding the null terminator byte. For example:
25/02/2025 10
Programming Fundamentals using C
25/02/2025 11
Programming Fundamentals using C
25/02/2025 12
Programming Fundamentals using C
25/02/2025 13
Programming Fundamentals using C
%s conversion specifier:
Reads all characters until the first whitespace character
Stores the characters read in the char array identified by the corresponding
argument
Stores the null terminator in the char array after accepting the last character
Leaves the delimiting whitespace character and any subsequent characters in
the input buffer
25/02/2025 14
Programming Fundamentals using C
The scanf() function will stop accepting input after the character ‘y’ and stores:
The characters ' name is Arnold' remain in the input buffer.
25/02/2025 15
Programming Fundamentals using C
The scanf() function will stop accepting input after the character ‘n’ and stores:
The characters ' name is Arnold' remain in the input buffer.
25/02/2025 16
Programming Fundamentals using C
• Stores the characters read in memory locations starting with the address
passed to scanf
• Stores the null byte in the byte following that where scanf stored the last
character
• Leaves the delimiting character (here, '\n') in the input buffer
25/02/2025 17
Programming Fundamentals using C
Example 2:
25/02/2025 18
Programming Fundamentals using C
Why?
25/02/2025 19
Programming Fundamentals using C
scanf(…) - cont.
Some character specifiers used in the function scanf(): Set of character are
or not accepted.
Specifier Description
%[abcd] Searches the input field for any of the characters a, b, c, and d
%[^abcd] Searches the input field for any characters except a, b, c, and d
%[0-9] To catch all decimal digits
%[A-Z] Catches all uppercase letters
%[0-9A-Za-z] Catches all decimal digits and all letters
%[A-FT-Z] Catches all uppercase letters from A to F and from T to Z
25/02/2025 20
Programming Fundamentals using C
25/02/2025 21
Programming Fundamentals using C
25/02/2025 22
Programming Fundamentals using C
25/02/2025 23
Programming Fundamentals using C
Example:
We need functions for processing
arrays and string
25/02/2025 24
Programming Fundamentals using C
25/02/2025 25
Programming Fundamentals using C
strlen() function
The strlen() function calculates the length of a given string. It doesn’t count
the null character ‘\0’.
Syntax: int strlen(const char *str);
Example:
Output:
25/02/2025 26
Programming Fundamentals using C
strcpy() function
The strcpy() is a function used to copy one string to another
Syntax: char* strcpy(char* dest, const char* src);
Example:
Output:
25/02/2025 27
Programming Fundamentals using C
strcmp() function
This function lexicographically compares the first n characters from the two
null-terminated strings and returns an integer based on the outcome.
Syntax: int strcmp(const char *str1, const char *str2);
Example:
Output:
25/02/2025 28
Programming Fundamentals using C
strcat() function
The strcat() function is used for string concatenation. It will append a copy of
the source string to the end of the destination string.
Syntax: char* strcat(char* dest, const char* src);
Example: Output:
25/02/2025 29
Programming Fundamentals using C
strupr() function
The strupr() function is used to converts a given string to uppercase.
Syntax: char *strupr(char *str);
Example:
Output:
25/02/2025 30
Programming Fundamentals using C
strlwr() function
The strlwr() function is used to converts a given string to lowercase.
Syntax: char *strlwr(char *str);
Example:
Output:
25/02/2025 31
Programming Fundamentals using C
strstr() function
The strstr() function is used to search the first occurrence of a substring in
another string.
Syntax: char *strstr (const char *s1, const char *s2);
Example:
Output:
25/02/2025 32
Programming Fundamentals using C
strtok() function
The strtok() function is used to split the string into small tokens based on a
set of delimiter characters.
Syntax: char * strtok(char* str, const char *delims);
Example:
Output:
25/02/2025 33
Programming Fundamentals using C
25/02/2025 34
Programming Fundamentals using C
25/02/2025 35
Programming Fundamentals using C
25/02/2025 36
Programming Fundamentals using C
25/02/2025 37
Programming Fundamentals using C
25/02/2025 38
Programming Fundamentals using C
25/02/2025 39
Programming Fundamentals using C
Exercise 4:
Suppose that only the blank character is used to separate words in a sentence.
Implement a function for counting number of words in a sentence.
T n u n a n u n o n g t
i 0 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1
0 1 2 3 4 5 6 7 8 9
count 0 1 2 3 4 5 6
25/02/2025 40
Programming Fundamentals using C
Exercise 5:
1 2 n u a 7 n a 9 d f 1 2 3 4 5 9 P 7
0 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1
i 0 1 2 3 4 5 6 7 8 9
count 0 1 2 3 4 5
25/02/2025 41
Programming Fundamentals using C
Exercise 6: Do Yourself
25/02/2025 42
Programming Fundamentals using C
8. Array of Strings
A string array declaration takes the form
For example, to declare an array of 5 names, where each name holds up to
30 characters, we write:
char names[5][31];
or:
char names[5][31] = { "Harry", "Jean", "Jessica", "Irene", "Jim" };
25/02/2025 43
Programming Fundamentals using C
Initialization
25/02/2025 44
Programming Fundamentals using C
Output:
25/02/2025 45
Programming Fundamentals using C
Exercise 7:
Write a C program that will accept 10 names, print out the list, sort the list
using ascending order, print out the result.
Students complete this exercise based
on the code design prototype below.
Hint:
25/02/2025 46
Programming Fundamentals using C
25/02/2025 47
Programming Fundamentals using C
Summary
String in C is terminated by the NULL character (‘\0’)
A string is similar to an array of characters.
All input functions for string will automatically add the NULL character after
the content of the string.
Using the functions on arrays, strings are implemented to operate on arrays
and strings.
If dynamic arrays or strings (using pointers), the assignment can be used on
these pointers.
25/02/2025 48
Programming Fundamentals using C
Summary
String Input
• scanf; gets
String Functions and Arrays of Strings
• Functions: strlen(), strcpy(), strcmp(), strcat(), strupr(), strlwr(), strstr(), strtok()
Arrays of Strings
• Input and Output
• Passing to Functions