Uploaded by MANIKANDAN S

c examples

advertisement
1.Create a C program that takes a string input from the user and separates the
lowercase and uppercase characters into two separate strings. Then, display the
lowercase and uppercase characters separately.
1. Prompt User for Input: Start by asking the user to input a string. This can be achieved
using the printf function to display a message to the user.
2. Read User Input: Utilize the fgets function to read the input string provided by the
user. Store this input string in a character array (char str[100]).
3. Separate Lowercase and Uppercase Characters: Iterate through each character in
the input string using a loop. For each character:
4. Check if it is a lowercase character using the islower function from the <ctype.h>
library. If it is lowercase, append it to the lowercase string array.
5. Check if it is an uppercase character using the isupper function from the <ctype.h>
library. If it is uppercase, append it to the uppercase string array.
6. Null Terminate Strings: After separating the lowercase and uppercase characters,
add null terminators ('\0') to the end of both lowercase and uppercase string arrays to
ensure they are properly terminated.
7. Print Results: Display the separated lowercase and uppercase strings to the user
using the printf function.
Test Case 1:
Sample Input:
Enter the String:
BiT SaThY
Sample output:
Lowercase: iah
Uppercase: BTSTY
Test Case 2:
Sample Input:
Enter the String:
SathyMangaLam
Sample output:
Lowercase: athyangaam
Uppercase: SML
Test Case 3:
Sample Input:
Enter the String:
bannari AMMAN
Sample output:
Lowercase: bannari
Uppercase: AMMAN
2. Count the number of punctuation characters exists in a string
Write a function to count the number of punctuation characters present in a given string.
Punctuation characters are defined as any non-alphanumeric characters such as
commas, periods, exclamation marks, question marks, etc.
Test Case 1:
Input a string :
Car,@, Bike,@, Book
The punctuation characters exists in the string is : 6
Test Case 2:
Input a string :
5166,@46342355^%$&$*(^,(3163165)
The punctuation characters exists in the string is : 13
Test Case 3:
Input a string :
/-*/-/-/!@#$%^&*()BIT SATHY
The punctuation characters exists in the string is : 18
3. Remove characters in String Except Alphabets
1. Start by displaying information about the task to the user, informing them that the
program will remove characters in the string except alphabets. You can use printf
statements to achieve this.
2. Read Input String: Use the fgets function to read a string from the standard input
(keyboard) and store it in a character array (str). Ensure that the size of the input buffer
is specified to prevent buffer overflow.
3. Remove Non-Alphabetic Characters: Iterate through each character of the input
string using a for loop. For each character, check if it is an alphabetic character (either
lowercase or uppercase) using logical conditions. If the character is not an alphabetic
character, remove it from the string by shifting the subsequent characters one position
to the left. Continue this process until the end of the string is reached.
4. Ensure Proper Termination: After removing non-alphabetic characters from the string,
ensure that the string is properly terminated by adding a null terminator ('\0') at the end.
5. Print the modified string after removing non-alphabetic characters to provide
feedback to the user. Use printf statements for this purpose.
Test Case 1:
Remove characters in String Except Alphabets :
-------------------------------------------------Input the string :
Bit_sathy@123
After removing the Output String : Bitsathy
Test Case 2:
Remove characters in String Except Alphabets :
-------------------------------------------------Input the string :
12345C45454S9898E156
After removing the Output String : CSE
Test Case 3:
Remove characters in String Except Alphabets :
-------------------------------------------------Input the string :
32131Bannari@12134567@#$%Amman
After removing the Output String : BannariAmman
4. Reverse only letter
Given a string s, reverse the string according to the following rules:
All the characters that are not English letters remain in the same position.
All the English letters (lowercase or uppercase) should be reversed.
Return s after reversing it.
Constraints:
1 <= s.length <= 100
s consists of characters with ASCII values in the range [33, 122].
s does not contain '\"' or '\\'.
Test Case 1:
Input: s = "ab-cd"
Output: "dc-ba"
Explanation:
Reversed English letters: "dc-ba"
Non-English letters remain in the same position.
Test Case 2:
Input: s = "a-bC-dEf-ghIj"
Output: "j-Ih-gfE-dCba"
Explanation:
Reversed English letters: "j-Ih-gfE-dCba"
Non-English letters remain in the same position.
Test Case 3:
Input: s = "Test1ng-Leet=code-Q!"
Output: "Qedo1ct-eeLg=ntse-T!"
Explanation:
Reversed English letters: "Qedo1ct-eeLg=ntse-T!"
Non-English letters remain in the same position.
Output: "Qedo1ct-eeLg=ntse-T!"
5. Good or Bad String
In this problem, a String S is composed of lowercase alphabets and wildcard characters
i.e. ‘?’. Here, ‘?’ can be replaced by any of the lowercase alphabets.
Now you have to classify the given String on the basis of the following rules:
i. If there are more than 3 consonants together or more than 5 vowels together, the
String is considered to be “BAD”.
A String is considered “GOOD” only if it is not “BAD”.
NOTE: String is considered as “BAD” if the above condition is satisfied even once. Else
it is “GOOD” and the task is to make the string “BAD”.
Test Case 1:
Input:
S = aeioup??
Output: 1
Explanation: The String doesn’t contain more than 3 consonants or more than 5 vowels
together. So, it’s a GOOD string.
Test Case 2:
Input:
S = bcdaeiou??
Output: 0
Explanation: The String contains the Substring “aeiou??” which counts as 7 vowels
together. So, it’s a BAD string.
Test Case 3:
Input:
S = bcd??aeiou??
Output: 0
Explanation: The String contains the Substring “aeiou??” which counts as 7 vowels
together. and more than 5 consonants "bcd??"
So, it’s a BAD string.
6. Print Words Vertically
Given a string s. Return all the words vertically in the same order in which they appear
in s.
Words are returned as a list of strings, complete with spaces when necessary. (Trailing
spaces are not allowed).
Each word would be put on only one column and that in one column there will be only
one word.
Constraints:
1 <= s.length <= 200
s contains only uppercase English letters.
It's guaranteed that there is only one space between 2 words.
Test Case 1:
Input: s = "HOW ARE YOU"
Output: ["HAY","ORO","WEU"]
Explanation: Each word is printed vertically.
"HAY"
"ORO"
"WEU"
Test Case 2:
Input: s = "TO BE OR NOT TO BE"
Output: ["TBONTB","OEROOE"," T"]
Explanation: Trailing spaces are not allowed.
"TBONTB"
"OEROOE"
" T"
Test Case 3:
Input: s = "CONTEST IS COMING"
Output: ["CIC","OSO","N M","T I","E N","S G","T"]
7. Longest palindrome substring
Given a string s , return the longest palindrome substring in s
Test Case 1:
Input: s = “babad”
Output: “bab”
Explanation:"aba" is also a valid answer.
Test Case 2:
Input: s = “cbbd”
Output: “bb”
Test Case 3:
Input s = “aab”
Output : “aa”
8. Parenthesis are balanced or not
You are given a string made up of parenthesis only.Your task is to check whether
parentheses are balanced or not.If they are balanced print 1 else print 0
Test Case 1:
Sample Input :
{({})}
Sample Output :
1
Test Case 2:
Sample Input :
{({)}
Sample Output :
0
Test Case 3:
Sample Input :
{({)}}}
Sample Output :
0
Download