SlideShare a Scribd company logo
1 of 24
U N I T I I I
S T R I N G
P.Ananthi, Assistant Professor, Kongu Engineering
College 1
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
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
• 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
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
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
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
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
#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
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
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
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
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
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
strchr String Character Search char *ptr = strchr("Hello, World!", 'W');
strstr String Substring Search char *ptr = strstr("Hello, World!", "World");
strtok String Tokenization
char str[] = "apple,orange,banana";
char *token = strtok(str, ",");
while (token != NULL)
{ /* process token */ token = strtok(NULL, ","); }
15
P.Ananthi, Assistant Professor, Kongu Engineering
College
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
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
// 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
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
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
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
toupper
Convert Character to
Uppercase
toupper('b') returns 'B'
tolower
Convert Character to
Lowercase
tolower('Z') returns 'z'
22
P.Ananthi, Assistant Professor, Kongu Engineering
College
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
#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

More Related Content

What's hot

Pointers in c
Pointers in cPointers in c
Pointers in c
Mohd Arif
 
C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
verisan
 
structure and union
structure and unionstructure and union
structure and union
student
 

What's hot (20)

Pointer in c
Pointer in cPointer in c
Pointer in c
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers
PointersPointers
Pointers
 
Ponters
PontersPonters
Ponters
 
Unit 8. Pointers
Unit 8. PointersUnit 8. Pointers
Unit 8. Pointers
 
pointers
pointerspointers
pointers
 
Introduction to pointers and memory management in C
Introduction to pointers and memory management in CIntroduction to pointers and memory management in C
Introduction to pointers and memory management in C
 
C pointers
C pointersC pointers
C pointers
 
PART 7 - Python Tutorial | Dictionaries In Python With Examples
PART 7 - Python Tutorial | Dictionaries In Python With ExamplesPART 7 - Python Tutorial | Dictionaries In Python With Examples
PART 7 - Python Tutorial | Dictionaries In Python With Examples
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
PART 3 - Python Tutorial | For Loop In Python With Examples
PART 3 - Python Tutorial | For Loop In Python With ExamplesPART 3 - Python Tutorial | For Loop In Python With Examples
PART 3 - Python Tutorial | For Loop In Python With Examples
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
Smart pointers
Smart pointersSmart pointers
Smart pointers
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
 
C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
structure and union
structure and unionstructure and union
structure and union
 

Similar to String.pptx

CPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPTCPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPT
Sasideepa
 

Similar to String.pptx (20)

COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Strings
StringsStrings
Strings
 
Team 1
Team 1Team 1
Team 1
 
CP-STRING (1).ppt
CP-STRING (1).pptCP-STRING (1).ppt
CP-STRING (1).ppt
 
CP-STRING.ppt
CP-STRING.pptCP-STRING.ppt
CP-STRING.ppt
 
CP-STRING.ppt
CP-STRING.pptCP-STRING.ppt
CP-STRING.ppt
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
CPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPTCPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPT
 
BHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPTBHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPT
 
c programming
c programmingc programming
c programming
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
 
Strings v.1.1
Strings v.1.1Strings v.1.1
Strings v.1.1
 
String notes
String notesString notes
String notes
 

Recently uploaded

AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 

Recently uploaded (20)

PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 

String.pptx

  • 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
  • 15. strchr String Character Search char *ptr = strchr("Hello, World!", 'W'); strstr String Substring Search char *ptr = strstr("Hello, World!", "World"); strtok String Tokenization char str[] = "apple,orange,banana"; char *token = strtok(str, ","); while (token != NULL) { /* process token */ token = strtok(NULL, ","); } 15 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
  • 22. toupper Convert Character to Uppercase toupper('b') returns 'B' tolower Convert Character to Lowercase tolower('Z') returns 'z' 22 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