w3resource

C Programming Exercises, Practice, Solution : String

C String [41 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

1. Write a program in C to input a string and print it. Go to the editor

Test Data :
Input the string : Welcome, w3resource

Expected Output :

The string you entered is : Welcome, w3resource 

Click me to see the solution

2. Write a program in C to find the length of a string without using library function. Go to the editor

Test Data :
Input the string : w3resource.com

Expected Output :

Length of the string is : 15 

Click me to see the solution

3. Write a program in C to separate the individual characters from a string. Go to the editor

Test Data :
Input the string : w3resource.com

Expected Output :

The characters of the string are : 
w  3  r  e  s  o  u  r  c  e  .  c  o  m 

Click me to see the solution

4. Write a program in C to print individual characters of string in reverse order. Go to the editor

Test Data :
Input the string : w3resource.com

Expected Output :

The characters of the string in reverse are :
m  o  c  .  e  c  r  u  o  s  e  r  3  w 

Click me to see the solution

5. Write a program in C to count the total number of words in a string. Go to the editor

Test Data :
Input the string : This is w3resource.com

Expected Output :

Total number of words in the string is : 3 

Click me to see the solution

6. Write a program in C to compare two strings without using string library functions. Go to the editor

Test Data :
Check the length of two strings:
--------------------------------
Input the 1st string : aabbcc
Input the 2nd string : abcdef
String1: aabbcc
String2: abcdef
Expected Output : Strings are not equal.

Check the length of two strings:
--------------------------------
Input the 1st string : aabbcc
Input the 2nd string : aabbcc
String1: aabbcc
String2: aabbcc
Expected Output : Strings are equal.

Click me to see the solution

7. Write a program in C to count total number of alphabets, digits and special characters in a string. Go to the editor

Test Data :
Input the string : Welcome to w3resource.com

Expected Output :

Number of Alphabets in the string is : 21 
Number of Digits in the string is : 1 
Number of Special characters in the string is : 4 

Click me to see the solution

8. Write a program in C to copy one string to another string. Go to the editor

Test Data :
Input the string : This is a string to be copied.

Expected Output :

The First string is : This is a string to be copied.
The Second string is : This is a string to be copied. 
Number of characters copied : 31 

Click me to see the solution

9. Write a program in C to count total number of vowel or consonant in a string. Go to the editor

Test Data :
Input the string : Welcome to w3resource.com

Expected Output :

The total number of vowel in the string is : 9 
The total number of consonant in the string is : 12 

Click me to see the solution

10. Write a program in C to find maximum occurring character in a string. Go to the editor

Test Data :
Input the string : Welcome to w3resource.com.

Expected Output :

The Highest frequency of character 'e' 
appears number of times : 4 

Click me to see the solution

11. Write a C program to sort a string array in ascending order. Go to the editor

Test Data :
Input the string : w3resource

Expected Output :

After sorting the string appears like : 
3ceeorrsuw 

Click me to see the solution

12. Write a program in C to read a string through keyboard and sort it using bubble sort. Go to the editor

Test Data :
Input number of strings :3
Input string 3 :
zero
one
two

Expected Output :

The strings appears after sorting : 
one 
two 
zero 

Click me to see the solution

13. Write a program in C to extract a substring from a given string. Go to the editor

Test Data :
Input the string : this is test string
Input the position to start extraction :9
Input the length of substring :4

Expected Output :

The substring retrieve from the string is : " test " 

Click me to see the solution

14. Write a C program to check whether a given substring is present in the given string. Go to the editor

Test Data :
Input the string : This is a test string.
Input the substring to be search : search

Expected Output :

The substring is not exists in the string.

Click me to see the solution

15. Write a program in C to read a sentence and replace lowercase characters by uppercase and vice-versa. Go to the editor

Test Data :
Input the string : This Is A Test String.

Expected Output :

The given sentence is   : This Is A Test String.
After Case changed the string  is: tHIS iS a tEST sTRING.

Click me to see the solution

16. Write a program in C to find the number of times a given word 'the' appears in the given string. Go to the editor

Test Data :
Input the string : The string where the word the present more than once.

Expected Output :

The frequency of the word 'the' is : 3 

Click me to see the solution

17. Write a program in C to remove characters in String Except Alphabets. Go to the editor

Test Data :
Input the string : w3resource.com

Expected Output :

After removing the Output String : wresourcecom 

Click me to see the solution

18. Write a program in C to Find the Frequency of Characters. Go to the editor

Test Data :
Input the string : This is a test string
Input the character to find frequency: i

Expected Output :

The frequency of 'i' is : 3 

Click me to see the solution

19. Write a program in C to Concatenate Two Strings Manually. Go to the editor

Test Data :
Input the first string : this is string one
Input the second string : this is string two

Expected Output :

After concatenation the string is : 
this is string one this is string two

Click me to see the solution

20. Write a program in C to find the largest and smallest word in a string. Go to the editor

Test Data :
Input the string : It is a string with smallest and largest word.

Expected Output :

The largest word is 'smallest'
and the smallest word is 'a' 
in the string : 'It is a string with smallest and largest word.'.

Click me to see the solution

21. Write a program in C to convert a string to uppercase. Go to the editor

Test Data :
Input a string in lowercase : the quick brown fox jumps over the lazy dog

Expected Output :

Here is the above string in UPPERCASE :                                
 THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
 

Click me to see the solution

22. Write a program in C to convert a string to lowercase. Go to the editor

Test Data :
Input a string in UPPERCASE : THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.

Expected Output :

Here is the above string in lowercase :                              
the quick brown fox jumps over the lazy dog.

Click me to see the solution

23. Write a program in C to check whether a character is Hexadecimal Digit or not. Go to the editor

Test Data :
Input a character : 7

Expected Output :

The entered character is a hexadecimal digit.

Click me to see the solution

24. Write a program in C to check whether a letter is uppercase or not. Go to the editor

Test Data :
Input a character : p

Expected Output :

The entered letter is not an UPPERCASE letter.

Click me to see the solution

25. Write a program in C to replace the spaces of a string with a specific character. Go to the editor

Test Data :
Input a string : Be glad to see the back of Input replace character : *

Expected Output :

After replacing the space with  * the new string is :                 
Be*glad*to*see*the*back*of*

Click me to see the solution

26. Write a program in C to count the number of punctuation characters exists in a string. Go to the editor

Test Data :
Input a string : The quick brown fox, jumps over the, lazy dog.

Expected Output :

The punctuation characters exists in the string is : 3 

Click me to see the solution

27. Write a program in C to print only the string before new line character. Go to the editor

Note: isprint() will only print line one, because the newline character is not printable.

Expected Output :

The quick brown fox

Click me to see the solution

28. Write a program in C to check whether a letter is lowercase or not. Go to the editor

Test Data :
Input a character : w

Expected Output :

The entered letter is a lowercase letter.

Click me to see the solution

29. Write a program in C to read a file and remove the spaces between two words of its content. Go to the editor

Expected Output :

The content of the file is :                                           
The quick brown fox jumps over the lazy dog                            
After removing the spaces the content is :                             
Thequickbrownfoxjumpsoverthelazydog

Click me to see the solution

30. Write a program in C to check whether a character is digit or not. Go to the editor

Test Data :
Input a character : 8

Expected Output :

The entered character is a digit.

Click me to see the solution

31. Write a program in C to split string by space into words. Go to the editor

Test Data :
Input a string : this is a test string

Expected Output :

Strings or words after split by space are : 
this is a test string .

Click me to see the solution

32. Write a C programming to find the repeated character in a given string. Go to the editor

Test Data :
Input a string: w3resource

Expected Output:

Input a string: The first repetitive character in w3resource is: r 

Click me to see the solution

33. Write a C programming to count of each character in a given string. Go to the editor

Test Data :
Input a string: w3resource

Expected Output:

Enter a str1ing: The count of each character in the string w3resource is 
w	1
3	1
r	2
e	2
s	1
o	1
u	1
c	1

Click me to see the solution

34. Write a C programming to convert vowels into upper case character in a given string. Go to the editor

Test Data :
Input a string : w3resource

Expected Output:

Input a sentence: The original string:
w3resource
After converting vowels into upper case the sentence becomes:
w3rEsOUrcE

Click me to see the solution

35. Write a C programming to find the length of the longest substring of a given string without repeating character. Go to the editor

Test Data :
Input a string: “abcddefffd”

Expected Output:

Input a string: Length of the longest substring without repeating characters: 4

Click me to see the solution

36. A given string contains the bracket characters '(', ')', '{', '}', '<', ‘>', '[' and ']', Write a C programme to check the said string is valid or not. The input string will be valid when open brackets and closed brackets are same type of brackets. Go to the editor

Test Data :
Input a string: <>()[]{}

Expected Output:

Check bracket in the said string is valid or not? 1

Click me to see the solution

37. Write a C programming to multiple two given positive numbers represent as string. Return a string representation of the product. Go to the editor

Expected Output:

Original numbers: 100 and 15
Multiple two said numbers represent as string? 1500

Click me to see the solution

38. Write a C programming to reverse all the vowels present in a given string. Return the new string. Go to the editor

Test Data :
Input a string: “AEIou”

Expected Output:

Input a string: Check bracket in the said string is valid or not? “uoIEA”

Click me to see the solution

39. Write a C programming to find the longest Palindromic substring from a given string. Return the substring. Go to the editor

Expected Output:

Original string: abcdcsdfabbccb
Longest Palindromic Substring from the said string? bccb

Click me to see the solution

40. Write a C programming to replace each lowercase letter with the same uppercase letter of a given string. Return the new string. Go to the editor

Sample Data:
("Python") -> "PYTHON"
("abcdcsd") -> "ABCDCSD"

Click me to see the solution

41. Write a C programming to calculate the length of longest common subsequence of two given strings. The strings consist of alphabetical characters. Go to the editor

Sample Data:
("abcdkiou", "cabsdf") -> 3
("pqrjad", "qr") -> 2

Click me to see the solution

C Programming Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Share this Tutorial / Exercise on : Facebook and Twitter

C Programming: Tips of the Day

~x + ~y == ~(x + y) is always false?

Assume for the sake of contradiction that there exists some x and some y (mod 2n) such that

~(x+y) == ~x + ~y

By two's complement*, we know that,

-x == ~x + 1
<==>  -1 == ~x + x 

Noting this result, we have,

~(x+y) == ~x + ~y
<==>  ~(x+y) + (x+y) == ~x + ~y + (x+y)
<==>  ~(x+y) + (x+y) == (~x + x) + (~y + y)
<==>  ~(x+y) + (x+y) == -1 + -1
<==>  ~(x+y) + (x+y) == -2
<==>  -1 == -2 

Hence, a contradiction. Therefore, ~(x+y) != ~x + ~y for all x and y (mod 2n).

*It is interesting to note that on a machine with one's complement arithmetic, the equality actually holds true for all x and y. This is because under one's complement, ~x = -x. Thus, ~x + ~y == -x + -y == -(x+y) == ~(x+y).

Ref : https://bit.ly/3EcHJb3