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
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
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.
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.
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()
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
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.
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:
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
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
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