0% found this document useful (0 votes)
11 views

Exercises Lecture 4

Uploaded by

nerf quta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Exercises Lecture 4

Uploaded by

nerf quta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Array Exercises

1. Find the Maximum Element in an Array


Write a C++ program that takes an array of integers as input and finds the maximum
element in the array.
Example Input: {3, 1, 5, 7, 2}
Output: 7
2. Count Even and Odd Numbers in an Array
Write a C++ program that counts how many even and odd numbers are in a given array of
integers.
Example Input: {1, 2, 3, 4, 5, 6, 7}
Output: Even numbers: 3, Odd numbers: 4
3. Reverse an Array
Write a C++ program that reverses the given array of integers without using built-in
functions.
Example Input: {1, 2, 3, 4, 5}
Output: {5, 4, 3, 2, 1}
4. Find the Second Largest Element in an Array
Write a C++ program to find the second largest element in an array.
Example Input: {1, 3, 4, 2, 7, 5}
Output: 5
5. Check if an Array is Sorted in Ascending Order
Write a C++ program that checks whether a given array is sorted in ascending order.
Example Input: {1, 2, 3, 4, 5}
Output: Array is sorted
String Exercises
1. Count Vowels in a String
Write a C++ program that counts how many vowels (a, e, i, o, u) are present in a given string.
Example Input: "Hello World"
Output: 3
2. Reverse a String
Write a C++ program that reverses a given string without using built-in functions.
Example Input: "Hello"
Output: "olleH"
3. Check if a String is Palindrome
Write a C++ program that checks if a given string is a palindrome (reads the same forwards
and backwards).
Example Input: "madam"
Output: Yes, it is a palindrome
4. Count the Frequency of Each Character in a String
Write a C++ program to count the frequency of each character in a string and display the
results.
Example Input: "aabbcc"
Output: a: 2, b: 2, c: 2
5. Remove All Whitespaces from a String
Write a C++ program that removes all whitespaces from a given string.
Example Input: "H e llo W orld"
Output: "HelloWorld"

You might also like