SlideShare a Scribd company logo
Session 7: CHARACTER ARRAYS AND STRING
Prepared by: Faculty 4.0 Team
C Programming-Special Training
(Crash Course)
Contents
2
1
3
4
2
Introduction
Character Array
Strings Handling
Functions
Problem Solving
INTRODUCTION
3
◉ A String is defined as a group of characters, digits and symbols.
◉ Example:
◉ In C, string is represented as
4
Character Array
DECLARATION
5
SYNTAX:
char variable_name[size];
Examples
char city[10];
char name[30];
size = max number of
characters + 1 byte
for the null character.
Null character=‘0’
Represents end of the string
DECLARATION & INITIALIZATION
6
SYNTAX:
char variable_name[size] = {list of characters};
Example
char city [8]={‘C’,‘H’,‘E’,‘N’,‘N’,‘A’,‘I’,‘0’};
/*NULL char need to be specified explicitly*/
C H E N N A I ‘0’
sizeof(city)? 8
No of characters?
7
DECLARATION & INITIALIZATION
7
SYNTAX:
char variable_name[size] = “string”;
Example
char city [8] = “CHENNAI”; /*NULL char appended implicitly*/
When compiler assigns a string to a character array, it automatically supplies a null character (‘0 ’)
at the end of the string.
C H E N N A I ‘0’
GETTING STRING AS INPUT FROM KEYBOARD
8
char str1[10];
scanf(“%s,str1);
scanf(“%s”,&str1)
char str1[50];
scanf(“%[^n]s,str1);
gets(str1);
BOTH REPRESENTS
BASE ADDRESS
[^n]-delimiter
2D CHARACTER ARRAYS
9
char str3[3][8]={“GOD”,”FELLOW”,”WORK”};
str[0] G O D 0
str[1] F E L L O W 0
str[2] W O R K 0
How to print
‘fellow’ alone?
How to print
‘f’ alone?
printf(“%s”,str[1]);
printf(“%c”,str[1][0]);
10
Problem Statement: Write a program to find the length of a string
Objective: Create a program that accepts a string input from the user and
then calculates and displays the length of the string.
Details:
•The string can be any sequence of characters.
•The length should reflect the total number of characters in the string,
including spaces and punctuation.
Input:
•A string entered by the user.
Output:
•The length of the string.
Examples:
Input: "Hello, world!"
Output: 13
Input: “vijayakumar"
Output: 11
11
12
Problem Statement: "Print Each Word on a New Line"
Objective: Write a program that takes a sentence as input and prints each word of the sentence on a
new line.
Input:
•A string s representing a sentence.
Output:
•Each word from the sentence printed on a separate line.
Details:
•The sentence will consist of words separated by spaces.
•You should ignore any leading or trailing spaces in the sentence.
•Assume the sentence contains only alphabetic characters and spaces; no punctuation marks are
included.
Example:
Input: "Hello world this is an example"
Output:
Hello
world
this
is
an
example
13
14
Problem Statement: "Counting Vowel Trees in the Garden"
Objective: Help Monk count the number of trees in the garden that have
vowels on them. These trees are not in good state and need care.
Input:
•A string trees representing the sequence of trees in the garden, where each
character corresponds to an English alphabet letter on a tree.
Output:
•An integer representing the count of trees with vowels (a, e, i, o, u) on them.
Details:
•The string will only contain lowercase English letters.
•Each letter in the string represents a tree in the garden.
Example:
Input: "abcefg"
Output: 2
Explanation:
The letters 'a' and 'e' are vowels, so there are 2 trees with vowels on them in
this example.
15
16
Problem Statement: "Digit Frequency in a String"
Objective: Write a program that determines the frequency of each digit (0 through
9) in a given string that contains both alphabets and numerical digits.
Input:
•A string s that may consist of lowercase alphabets and digits.
Output:
•A list or an array of ten integers that represent the frequencies of the digits 0
through 9 in the string. The index of the list corresponds to the digit.
Details:
•The string can be of any length.
•If a digit does not appear in the string, its frequency should be reported as 0.
•Ignore all non-digit characters in the string.
Example:
Input: "a123b45c6789"
Output: [0, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Explaination: The digit '1' appears once, '2' appears once, '3' appears once,
and so on up to '9'. The digit '0' does not appear at all.
17
String Handling Functions
18
 We need to often manipulate strings according to the
need of a problem.
 Most, if not all, of the time string manipulation can be
done manually but, this makes programming complex
and large.
 To solve this, C supports a large number of string
handling functions in the standard library "string.h".
String Handling Functions
19
 Following are the popular String handling functions
available in String.h header file.
strcat()
strcmp()
strcpy()
strlen()
20
Problem Statement
21
Write a program to reverse a collection of strings stored in
an 2D array.
Input:
2
Vijayakumar
Rathakrishnan
Output:
ramukayajiV
nanhsirkahtaR
22
Problem Statement
23
Problem Statement: "What is your mobile number?"
Bechan Chacha is feeling down because his crush gave him a list of
mobile numbers, some of which are valid and some are not. To cheer him
up and help him pick his crush's number correctly, you need to
determine which numbers from the list are valid.
Requirements for a valid mobile number:
The number must be exactly 10 digits long.
It should consist only of numeric values (0-9).
It must not have any leading zeros.
Input:
A string "S" representing the mobile number.
Output:
Return "Valid" if the number meets all the criteria.
Return "Invalid" if it does not meet one or more of the criteria.
24
25
Problem Statement: "Identifying the Most Frequent Character"
Objective: Write a program to identify the most frequently occurring character
in a given string. If there is more than one character with the same highest
frequency, return the lexicographically smallest character among them.
Input:
•A string s that may contain both uppercase and lowercase English letters.
Output:
•The character that appears most frequently in the string. If there is a tie in
frequency, return the lexicographically smallest character among those with the
highest frequency.
Details:
•The string s will not contain any numbers, special characters, or punctuation
marks, only English letters.
•Consider uppercase and lowercase letters as same characters.
Example:
26
27
Problem Statement: Sorting a List of Names
Description:
You are given a list of names (strings), and your task is to sort them in
lexicographical (alphabetical) order. Write a program that accepts multiple
names, sorts them, and then displays the sorted list.
Input:
•An integer N representing the number of names.
•Followed by N lines, each containing a name (no spaces within a name).
Output:
• Display the names in sorted order, one per line.
Input:
5
Vijay
Kumar
Divya
Bharathi
Sravan
Output:
Bharathi
Divya
Kumar
Sravan
Vijay
28
29
Problem Statement: Identify Palindrome Names
Description:
A palindrome is a word that reads the same backward as forward (e.g., "madam",
"level"). You are given a list of names, and your task is to identify which names are
palindromes.
Input:
•An integer N representing the number of names.
•Followed by N lines, each containing a name.
Output:
• Print each palindrome name from the list, one per line.
• If no palindrome names are found, print "No palindrome names".
Input:
5
radar
hello
level
world
madam
Output:
radar
level
madam
30
31
Problem Statement: Count Alphabets and Digits in a Text
Description:
Liam, a software developer, is building a text analysis tool for a language-learning
application. One of the features requires analyzing user-inputted text to determine
how many alphabet characters, Spaces and digits are present.
Write a C program that:
Accepts a string of text input from the user.
Counts and displays:
The number of alphabetic characters (both uppercase and lowercase).
The number of digits (0–9).
The number of Spaces.
Input:
A single line of text (can include letters, digits, spaces, and symbols).
Output:
Two numbers printed on separate lines:
Number of alphabets
Number of digits
Example:
Vijayakumar CS176
Alphabets : 13
Digits : 3
Spaces : 1
32
Thank You
33

More Related Content

PDF
Python data handling
PDF
0-Slot21-22-Strings.pdf
PDF
stringsinpython-181122100212.pdf
PPTX
String in programming language in c or c++
PPTX
PDF
Lexical analysis Compiler design pdf to read
PDF
Lexical analysis compiler design to read and study
Python data handling
0-Slot21-22-Strings.pdf
stringsinpython-181122100212.pdf
String in programming language in c or c++
Lexical analysis Compiler design pdf to read
Lexical analysis compiler design to read and study

Similar to 07-Strings( string Functions and parameters C).pdf (20)

PPTX
Learn C LANGUAGE at ASIT
PDF
python1uhaibueuhERADGAIUSAERUGHw9uSS.pdf
PDF
Strings in python
PPSX
Programming in c
PPTX
Data Types and Variables In C Programming
PDF
Acm aleppo cpc training third session
PDF
Introduction of c_language
PDF
Strings in c mrs.sowmya jyothi
PPT
All C ppt.ppt
PPT
C presentation book
PPTX
C - String ppt
PDF
14-Strings-In-Python strings with oops .pdf
DOCX
For this assignment, download the A6 code pack. This zip fil.docx
PDF
C Tutorial
PDF
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
PPTX
[Compilers23] Lexical Analysis – Scanning Part I.pptx
PPTX
ARRAY's in C Programming Language PPTX.
PPTX
Java Object Orientend Programming 1.pptx
PPTX
Strings in c++
Learn C LANGUAGE at ASIT
python1uhaibueuhERADGAIUSAERUGHw9uSS.pdf
Strings in python
Programming in c
Data Types and Variables In C Programming
Acm aleppo cpc training third session
Introduction of c_language
Strings in c mrs.sowmya jyothi
All C ppt.ppt
C presentation book
C - String ppt
14-Strings-In-Python strings with oops .pdf
For this assignment, download the A6 code pack. This zip fil.docx
C Tutorial
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
[Compilers23] Lexical Analysis – Scanning Part I.pptx
ARRAY's in C Programming Language PPTX.
Java Object Orientend Programming 1.pptx
Strings in c++
Ad

Recently uploaded (20)

PDF
PPT on Performance Review to get promotions
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Well-logging-methods_new................
PPTX
Sustainable Sites - Green Building Construction
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Geodesy 1.pptx...............................................
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPT on Performance Review to get promotions
UNIT 4 Total Quality Management .pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Model Code of Practice - Construction Work - 21102022 .pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
Well-logging-methods_new................
Sustainable Sites - Green Building Construction
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Geodesy 1.pptx...............................................
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Automation-in-Manufacturing-Chapter-Introduction.pdf
Safety Seminar civil to be ensured for safe working.
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
bas. eng. economics group 4 presentation 1.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Ad

07-Strings( string Functions and parameters C).pdf

  • 1. Session 7: CHARACTER ARRAYS AND STRING Prepared by: Faculty 4.0 Team C Programming-Special Training (Crash Course)
  • 3. INTRODUCTION 3 ◉ A String is defined as a group of characters, digits and symbols. ◉ Example: ◉ In C, string is represented as
  • 5. DECLARATION 5 SYNTAX: char variable_name[size]; Examples char city[10]; char name[30]; size = max number of characters + 1 byte for the null character. Null character=‘0’ Represents end of the string
  • 6. DECLARATION & INITIALIZATION 6 SYNTAX: char variable_name[size] = {list of characters}; Example char city [8]={‘C’,‘H’,‘E’,‘N’,‘N’,‘A’,‘I’,‘0’}; /*NULL char need to be specified explicitly*/ C H E N N A I ‘0’ sizeof(city)? 8 No of characters? 7
  • 7. DECLARATION & INITIALIZATION 7 SYNTAX: char variable_name[size] = “string”; Example char city [8] = “CHENNAI”; /*NULL char appended implicitly*/ When compiler assigns a string to a character array, it automatically supplies a null character (‘0 ’) at the end of the string. C H E N N A I ‘0’
  • 8. GETTING STRING AS INPUT FROM KEYBOARD 8 char str1[10]; scanf(“%s,str1); scanf(“%s”,&str1) char str1[50]; scanf(“%[^n]s,str1); gets(str1); BOTH REPRESENTS BASE ADDRESS [^n]-delimiter
  • 9. 2D CHARACTER ARRAYS 9 char str3[3][8]={“GOD”,”FELLOW”,”WORK”}; str[0] G O D 0 str[1] F E L L O W 0 str[2] W O R K 0 How to print ‘fellow’ alone? How to print ‘f’ alone? printf(“%s”,str[1]); printf(“%c”,str[1][0]);
  • 10. 10 Problem Statement: Write a program to find the length of a string Objective: Create a program that accepts a string input from the user and then calculates and displays the length of the string. Details: •The string can be any sequence of characters. •The length should reflect the total number of characters in the string, including spaces and punctuation. Input: •A string entered by the user. Output: •The length of the string. Examples: Input: "Hello, world!" Output: 13 Input: “vijayakumar" Output: 11
  • 11. 11
  • 12. 12 Problem Statement: "Print Each Word on a New Line" Objective: Write a program that takes a sentence as input and prints each word of the sentence on a new line. Input: •A string s representing a sentence. Output: •Each word from the sentence printed on a separate line. Details: •The sentence will consist of words separated by spaces. •You should ignore any leading or trailing spaces in the sentence. •Assume the sentence contains only alphabetic characters and spaces; no punctuation marks are included. Example: Input: "Hello world this is an example" Output: Hello world this is an example
  • 13. 13
  • 14. 14 Problem Statement: "Counting Vowel Trees in the Garden" Objective: Help Monk count the number of trees in the garden that have vowels on them. These trees are not in good state and need care. Input: •A string trees representing the sequence of trees in the garden, where each character corresponds to an English alphabet letter on a tree. Output: •An integer representing the count of trees with vowels (a, e, i, o, u) on them. Details: •The string will only contain lowercase English letters. •Each letter in the string represents a tree in the garden. Example: Input: "abcefg" Output: 2 Explanation: The letters 'a' and 'e' are vowels, so there are 2 trees with vowels on them in this example.
  • 15. 15
  • 16. 16 Problem Statement: "Digit Frequency in a String" Objective: Write a program that determines the frequency of each digit (0 through 9) in a given string that contains both alphabets and numerical digits. Input: •A string s that may consist of lowercase alphabets and digits. Output: •A list or an array of ten integers that represent the frequencies of the digits 0 through 9 in the string. The index of the list corresponds to the digit. Details: •The string can be of any length. •If a digit does not appear in the string, its frequency should be reported as 0. •Ignore all non-digit characters in the string. Example: Input: "a123b45c6789" Output: [0, 1, 1, 1, 1, 1, 1, 1, 1, 1] Explaination: The digit '1' appears once, '2' appears once, '3' appears once, and so on up to '9'. The digit '0' does not appear at all.
  • 17. 17
  • 18. String Handling Functions 18  We need to often manipulate strings according to the need of a problem.  Most, if not all, of the time string manipulation can be done manually but, this makes programming complex and large.  To solve this, C supports a large number of string handling functions in the standard library "string.h".
  • 19. String Handling Functions 19  Following are the popular String handling functions available in String.h header file. strcat() strcmp() strcpy() strlen()
  • 20. 20
  • 21. Problem Statement 21 Write a program to reverse a collection of strings stored in an 2D array. Input: 2 Vijayakumar Rathakrishnan Output: ramukayajiV nanhsirkahtaR
  • 22. 22
  • 23. Problem Statement 23 Problem Statement: "What is your mobile number?" Bechan Chacha is feeling down because his crush gave him a list of mobile numbers, some of which are valid and some are not. To cheer him up and help him pick his crush's number correctly, you need to determine which numbers from the list are valid. Requirements for a valid mobile number: The number must be exactly 10 digits long. It should consist only of numeric values (0-9). It must not have any leading zeros. Input: A string "S" representing the mobile number. Output: Return "Valid" if the number meets all the criteria. Return "Invalid" if it does not meet one or more of the criteria.
  • 24. 24
  • 25. 25 Problem Statement: "Identifying the Most Frequent Character" Objective: Write a program to identify the most frequently occurring character in a given string. If there is more than one character with the same highest frequency, return the lexicographically smallest character among them. Input: •A string s that may contain both uppercase and lowercase English letters. Output: •The character that appears most frequently in the string. If there is a tie in frequency, return the lexicographically smallest character among those with the highest frequency. Details: •The string s will not contain any numbers, special characters, or punctuation marks, only English letters. •Consider uppercase and lowercase letters as same characters. Example:
  • 26. 26
  • 27. 27 Problem Statement: Sorting a List of Names Description: You are given a list of names (strings), and your task is to sort them in lexicographical (alphabetical) order. Write a program that accepts multiple names, sorts them, and then displays the sorted list. Input: •An integer N representing the number of names. •Followed by N lines, each containing a name (no spaces within a name). Output: • Display the names in sorted order, one per line. Input: 5 Vijay Kumar Divya Bharathi Sravan Output: Bharathi Divya Kumar Sravan Vijay
  • 28. 28
  • 29. 29 Problem Statement: Identify Palindrome Names Description: A palindrome is a word that reads the same backward as forward (e.g., "madam", "level"). You are given a list of names, and your task is to identify which names are palindromes. Input: •An integer N representing the number of names. •Followed by N lines, each containing a name. Output: • Print each palindrome name from the list, one per line. • If no palindrome names are found, print "No palindrome names". Input: 5 radar hello level world madam Output: radar level madam
  • 30. 30
  • 31. 31 Problem Statement: Count Alphabets and Digits in a Text Description: Liam, a software developer, is building a text analysis tool for a language-learning application. One of the features requires analyzing user-inputted text to determine how many alphabet characters, Spaces and digits are present. Write a C program that: Accepts a string of text input from the user. Counts and displays: The number of alphabetic characters (both uppercase and lowercase). The number of digits (0–9). The number of Spaces. Input: A single line of text (can include letters, digits, spaces, and symbols). Output: Two numbers printed on separate lines: Number of alphabets Number of digits Example: Vijayakumar CS176 Alphabets : 13 Digits : 3 Spaces : 1
  • 32. 32