STRING BASICS
DECLARING AND INITIALIZING
POINTERS FOR STRING MANIPULATION
STRING HANDLING FUNCTIONS: https://onlinegdb.com/zaPP6-oMS
CHARACTER ORIENTED FUNCTIONS : https://onlinegdb.com/pp7DrUBRlc
TWO DIMENSIONAL ARRAY OF STRINGS
1. U N I T I I I
S T R I N G
P.Ananthi, Assistant Professor, Kongu Engineering
College 1
2. T O P I C S C O V E R E D
• STRING BASICS
• DECLARING AND INITIALIZING
• POINTERS FOR STRING MANIPULATION
• STRING HANDLING FUNCTIONS
• CHARACTER ORIENTED FUNCTIONS
• TWO DIMENSIONAL ARRAY OF STRINGS
2
P.Ananthi, Assistant Professor, Kongu Engineering
College
3. S T R I N G
• A string is a sequence of zero or more characters enclosed within double quotes.
• Strings are represented as array of characters. No separate data type is available in C
• Example: "CSD"
• String literals are enclosed with double quotes "CSD" where as characters literals are
enclosed in single quotes "C"
• Quotes are not part of string but are delimiters
• Every string constant is automatically terminated by the null character i.e '0' => ASCII
value is 0
3
P.Ananthi, Assistant Professor, Kongu Engineering
College
4. • The characters enclosed in double quotes and terminating null character are stored in
continuous memory location.
o EG: "RED CAR"
o The number of bytes required includes null character also. Memory required for string "RED
CAR" is 8
o The length of the string is represented by the number of characters present in the string 7.
R E D C A R '0'
4
P.Ananthi, Assistant Professor, Kongu Engineering
College
5. D E C L A R AT I O N & I N I T I A L I Z AT I O N
• Strings are represented as arrays of characters
• String initialization in C involves creating a character array to store a sequence of
characters, terminated by a null character ('0').
• %s format specifier is used for string.
char str[]="Computer";
char str[9]="Computer";
char str[9]={'C','o','m','p','u','t','e','r','0'}
5
P.Ananthi, Assistant Professor, Kongu Engineering
College
6. E X A M P L E 1
#include <stdio.h>
int main() {
// Using character arrays to represent strings
char str1[] = "Hello, World!"; // Automatically includes a null terminator '0'
char str2[12] = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '0'};
printf("str1: %sn", str1);
printf("str2: %sn", str2);
return 0;
}
6
P.Ananthi, Assistant Professor, Kongu Engineering
College
7. E X A M P L E 2
#include <stdio.h>
int main() {
// Reading a string from the user
char input[50];
printf("Enter a string: ");
scanf("%s", input); // Note: %s reads a string until a space is encountered
printf("You entered: %sn", input);
return 0;
}
7
P.Ananthi, Assistant Professor, Kongu Engineering
College
8. S T R I N G D E C L A R I N G U S I N G
P O I N T E R
#include <stdio.h>
int main() {
// String pointer initialization with a string literal
const char *strPtr = "Hello !";
// Printing the string using the pointer
printf("String: %sn", strPtr);
return 0;
} H E L L O ! 0
2000 2001 2002 2003 2004 2005 2006 2007
StrPtr points to 2000
8
P.Ananthi, Assistant Professor, Kongu Engineering
College
9. #include <stdio.h>
int main() {
// String pointer initialization with a character array
char message[] = "C Programming";
char *ptr = message;
// Printing the string using the pointer
printf("String: %sn", ptr);
return 0;
}
9
P.Ananthi, Assistant Professor, Kongu Engineering
College
10. PA S S I N G S T R I N G T O A F U N C T I O N
• Strings can be passed to a function in a similar way as arrays.
10
P.Ananthi, Assistant Professor, Kongu Engineering
College
11. PA S S I N G S T R I N G S T O A F U N C T I O N
#include <stdio.h>
void displayString(char str[]);
int main()
{
char str[50];
printf("Enter string: ");
• fgets(str, sizeof(str), stdin);
• displayString(str); //
Passing string to a function.
• return 0; }
• void displayString(char str[])
• { printf("String Output: ");
• puts(str);}
11
P.Ananthi, Assistant Professor, Kongu Engineering
College
12. S T R I N G R E A D I N G A N D W R I T I N G
Operation Function Example
Reading Strings
Read with scanf scanf("%s", str);
scanf reads a string until a
space is encountered.
Read with fgets fgets(str, size, stdin);
fgets reads a line with spaces,
ensuring safer input.
Read with gets gets(str);
gets reads a line but is
considered unsafe due to
potential buffer overflow.
12
P.Ananthi, Assistant Professor, Kongu Engineering
College
13. WRITING STRINGS
Write with printf printf("Message: %sn", str);
printf is commonly used for
formatted output.
Write with puts puts(str);
puts prints a string followed
by a newline.
Write with fputs fputs(str, stdout);
fputs writes a string to the
specified file stream (e.g.,
stdout for the console).
Write with fputs fputs(str, file);
fputs can also be used to
write to a file stream.
Write with puts puts(str);
puts prints a string followed
by a newline. It is different
from fputs as it automatically
appends a n
13
P.Ananthi, Assistant Professor, Kongu Engineering
College
14. S T R I N G H A N D L I N G F U N C T I O N S
• There are many predefined functions available for various strig operation.
Function Purpose Example
strlen String Length
size_t length = strlen("Hello,
World!");
strcpy String Copy
char dest[20]; strcpy(dest, "Copy
me!");
strcat String Concatenation
char str[20] = "Hello"; strcat(str, ",
World!");
strcmp String Comparison int result = strcmp("Hello", "World");
14
P.Ananthi, Assistant Professor, Kongu Engineering
College
16. Strlwr() String lower Can convert the string to lowercase
Strupr() String upper
Is used to convert the letters of string to
uppercase
Strrev() String reverse Is used to reverse the strin
16
P.Ananthi, Assistant Professor, Kongu Engineering
College
17. S T R I N G U S E R
D E F I N E D F U N C T I O N
• A user-defined function, also known as
a custom function or user function, is a
function that is created and defined by the
user to perform a specific task or set of tasks.
• <string.h> header file should be added to
work with user defined string functions
17
P.Ananthi, Assistant Professor, Kongu Engineering
College
18. // Function to count the number of vowels in a string
int countVowels(const char *str) {
int count = 0;
int length = strlen(str);
for (int i = 0; i < length; i++) {
char currentChar = tolower(str[i]); // Convert the character to lowercase for case-insensitivity
if (currentChar == 'a' || currentChar == 'e' || currentChar == 'i' || currentChar == 'o' || currentChar ==
'u') {
count++;
}
}
return count;
}
18
P.Ananthi, Assistant Professor, Kongu Engineering
College
19. C H A R A C T E R O R I E N T E D
F U N C T I O N S
• Character-oriented functions in C are those
that operate on individual characters within a
string. These functions are often used for
tasks such as checking character types,
converting case, and locating characters.
19
P.Ananthi, Assistant Professor, Kongu Engineering
College
20. unction Purpose Example
isspace Check if Character is a Space isspace(' ') returns true
ispunct
Check if Character is
Punctuation
ispunct('!') returns true
isxdigit Check if Character is Hex Digit isxdigit('A') returns true
isprint Check if Character is Printable isprint('a') returns true
iscntrl
Check if Character is a Control
Character
iscntrl('n') returns true
isgraph Check if Character is Graphical isgraph('A') returns true
20
P.Ananthi, Assistant Professor, Kongu Engineering
College
21. Function Purpose Example
isalpha Check if Character is Alphabetic isalpha('A') returns true
isdigit Check if Character is a Digit isdigit('5') returns true
isalnum
Check if Character is
Alphanumeric
isalnum('X') returns true
isupper
Check if Character is
Uppercase
isupper('Q') returns true
islower
Check if Character is
Lowercase
islower('z') returns true
21
P.Ananthi, Assistant Professor, Kongu Engineering
College
23. 2 D A R R AY O F
S T R I N G
• 2D array of strings in C is essentially an array
of arrays, where each element of the array is
itself an array of characters (a string)
23
P.Ananthi, Assistant Professor, Kongu Engineering
College
24. #include <stdio.h>
int main() {
// 2D array of strings
char strings[3][20]; // 3 strings,
each with a maximum length of 19
characters (plus '0')
// Assigning values to the 2D
array
strcpy(strings[0], "Apple");
strcpy(strings[1], "Orange");
strcpy(strings[2], "Banana");
// Displaying the strings
printf("Strings:n");
for (int i = 0; i < 3; i++) {
printf("%sn", strings[i]);
}
return 0;
}
24
P.Ananthi, Assistant Professor, Kongu Engineering
College