C String Exercises
Site: | Saylor Academy |
Course: | CS107: C++ Programming |
Book: | C String Exercises |
Printed by: | Guest user |
Date: | Friday, February 4, 2022, 5:17 PM |
Description
Complete these exercises to test your understanding of string arrays.
C String Exercises
Exercise 1
Write a program in C to input a string and print it.
Test Data:
Input the string: Welcome, w3resource
Expected Output:
The string you entered is: Welcome, w3resource
Exercise 2
Write a program in C to find the length of a string without using library function.
Test Data:
Input the string: w3resource.com
Expected Output:
Length of the string is: 15
Exercise 3
Write a program in C to separate the individual characters from a string.
Test Data:
Input the string: w3resource.com
The characters of the string are: w 3 r e s o u r c e . c o m
Exercise 4
Write a program in C to print individual characters of string in reverse order.
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
Exercise 5
Write a program in C to count the total number of words in a string.
Test Data:
Input the string: This is w3resource.com
Expected Output:
Total number of words in the string is: 3
Exercise 6
Write a program in C to compare two strings without using string library functions.
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.
Exercise 7
Write a program in C to count total number of alphabets, digits and special characters in a string.
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
Exercise 8
Write a program in C to copy one string to another string.
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
Exercise 9
Write a program in C to count total number of vowel or consonant in a string.
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
Exercise 10
Write a program in C to find maximum occurring character in a string.
Test Data:
Input the string: Welcome to w3resource.com.
Expected Output:
The Highest frequency of character 'e' appears number of times: 4
Exercise 11
Write a C program to sort a string array in ascending order.
Test Data:
Input the string: w3resource
Expected Output:
After sorting the string appears like: 3ceeorrsuw
Exercise 12
Write a program in C to read a string through keyboard and sort it using bubble sort.
Test Data:
Input number of strings :3
Input string 3:
zero
one
two
Expected Output:
The strings appears after sorting: one two zero
Exercise 13
Write a program in C to extract a substring from a given string.
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"
Exercise 14
Write a C program to check whether a given substring is present in the given string.
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.
Exercise 15
Write a program in C to read a sentence and replace lowercase characters by uppercase and vice-versa.
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.
Exercise 16
Write a program in C to find the number of times a given word 'the' appears in the given string.
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
Exercise 17
Write a program in C to remove characters in String Except Alphabets.
Test Data:
Input the string: w3resource.com
Expected Output:
After removing the Output String: wresourcecom
Exercise 18
Write a program in C to Find the Frequency of Characters.
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
Exercise 19
Write a program in C to Concatenate Two Strings Manually.
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
Exercise 20
Write a program in C to find the largest and smallest word in a string.
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.'.
Exercise 21
Write a program in C to convert a string to uppercase.
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.
Exercise 22
Write a program in C to convert a string to lowercase.
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.
Exercise 23
Write a program in C to check whether a character is Hexadecimal Digit or not.
Test Data:
Input a character: 7
Expected Output:
The entered character is a hexadecimal digit.
Exercise 24
Write a program in C to check whether a letter is uppercase or not.
Test Data:
Input a character: p
Expected Output:
The entered letter is not an UPPERCASE letter.
Exercise 25
Write a program in C to replace the spaces of a string with a specific character.
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*
Exercise 26
Write a program in C to count the number of punctuation characters exists in a string.
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
Exercise 27
Write a program in C to print only the string before new line character.
Note: isprint()
will only print line one, because the newline character is not printable.
Expected Output:
The quick brown fox
Exercise 28
Write a program in C to check whether a letter is lowercase or not.
Test Data:
Input a character: w
Expected Output:
The entered letter is a lowercase letter.
Exercise 29
Write a program in C to read a file and remove the spaces between two words of its content.
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
Exercise 30
Write a program in C to check whether a character is digit or not.
Test Data:
Input a character: 8
Expected Output:
The entered character is a digit.
Exercise 31
Write a program in C to split string by space into words.
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 .
Exercise 32
Write a C programming to find the repeated character in a given string.
Test Data:
Input a string: w3resource
Expected Output:
Input a string: The first repetitive character in w3resource is: r
Exercise 33
Write a C programming to count of each character in a given string.
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
Exercise 34
Write a C programming to convert vowels into upper case character in a given string.
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
Source: W3resource, https://www.w3resource.com/c-programming-exercises/string/index.php This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.
C String: Solutions
Exercise 1
Write a program in C to input a string and print it.
Sample Solution:
C Code:
#include <stdio.h>
#include <stdlib.h>
void main()
{
char str[50];
printf("\n\nAccept a string from keyboard:\n");
printf("-----------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
printf("The string you entered is: %s\n", str);
}
Sample Output:
Accept a string from keyboard:-----------------------------------
Input the string: Welcome, w3resource
The string you entered is: Welcome, w3resource
Flowchart:
Exercise 2
Write a program in C to find the length of a string without using library function.![C Programming: Find the length of a string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-2.png)
Sample Solution:
C Code:#include <stdio.h>
#include <stdlib.h>
void main()
{
char str[100]; /* Declares a string of size 100 */
int l= 0;
printf("\n\nFind the length of a string:\n");
printf("---------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
while(str[l]!='\0')
{
l++;
}
printf("Length of the string is: %d\n\n", l-1);
}
Sample Output:
Find the length of a string:---------------------------------
Input the string: w3resource.com
Length of the string is: 15
Flowchart:
![Flowchart: Find the length of a string.](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-2.png)
Exercise 3
Write a program in C to separate the individual characters from a string.![C Programming: Separate the individual characters from a string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-3.png)
Sample Solution:
C Code:#include <stdio.h>
#include <stdlib.h>
void main()
{
char str[100]; /* Declares a string of size 100 */
int l= 0;
printf("\n\nSeparate the individual characters from a string:\n");
printf("------------------------------------------------------\n");
printf("Input the string : ");
fgets(str, sizeof str, stdin);
printf("The characters of the string are: \n");
while(str[l]!='\0')
{
printf("%c ", str[l]);
l++;
}
printf("\n");
}
Sample Output:
Separate the individual characters from a string:------------------------------------------------------
Input the string: w3resource.com
The characters of the string are:
w 3 r e s o u r c e . c o m
Flowchart:
![Flowchart: Separate the individual characters from a string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-35fdc.png?time=1623438366072)
Exercise 4
Write a program in C to print individual characters of string in reverse order.![C Programming: Print individual characters of string in reverse order](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-4.png)
Sample Solution:
C Code:#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str[100]; /* Declares a string of size 100 */
int l,i;
printf("\n\nPrint individual characters of string in reverse order:\n");
printf("------------------------------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
l=strlen(str);
printf("The characters of the string in reverse are: \n");
for(i=l;i>=0;i--)
{
printf("%c ", str[i]);
}
printf("\n");
}
The program can also be written as below:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str[100]; /* Declares a string of size 100 */
int l=0;
printf("\n\nPrint individual characters of string in reverse order:\n");
printf("------------------------------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
l=strlen(str);
printf("The characters of the string in reverse are: \n");
for(str[l]='\0';l>=0;l--)
{
printf("%c ", str[l]);
}
printf("\n");
}
Sample Output:
Print individual characters of string in reverse order:-----------------------------------------------------------
Input the string: w3resource.com
The characters of the string in reverse are:
m o c . e c r u o s e r 3 w
Flowchart :
![Flowchart: Print individual characters of string in reverse order](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-487ca.png?time=1623438343240)
Exercise 5
Write a program in C to count the total number of words in a string.![C Programming: Count the total number of words in a string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-5.png)
Sample Solution:
C Code:#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define str_size 100 //Declare the maximum size of the string
void main()
{
char str[str_size];
int i, wrd;
printf("\n\nCount the total number of words in a string:\n");
printf("------------------------------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
i = 0;
wrd = 1;
/* loop till end of string */
while(str[i]!='\0')
{
/* check whether the current character is white space or new line or tab character*/
if(str[i]==' ' || str[i]=='\n' || str[i]=='\t')
{
wrd++;
}
i++;
}
printf("Total number of words in the string is: %d\n", wrd-1);
}
Sample Output:
Count the total number of words in a string:------------------------------------------------------
Input the string: This is w3resource.com
Total number of words in the string is: 3
Flowchart:
![Flowchart: Count the total number of words in a string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-5.png)
Exercise 6
Write a program in C to compare two strings without using string library functions.Sample Solution:
C Code:// C program to compare the two strings
// without using strcmp() function
#include <stdio.h>
#define str_size 100 //Declare the maximum size of the string
int test(char* s1, char* s2)
{
int flag = 0;
while (*s1 != '\0' || *s2 != '\0') {
if (*s1 == *s2) {
s1++;
s2++;
}
else if ((*s1 == '\0' && *s2 != '\0')
|| (*s1 != '\0' && *s2 == '\0')
|| *s1 != *s2) {
flag = 1;
break;
}
}
return flag;
}
int main(void)
{
char str1[str_size], str2[str_size];
int flg=0;
printf("\nInput the 1st string: ");
fgets(str1, sizeof str1, stdin);
printf("Input the 2nd string: ");
fgets(str2, sizeof str2, stdin);
printf("\nString1: %s", str1);
printf("String2: %s", str2);
flg = test(str1, str2);
if(flg == 0)
{
printf("\nStrings are equal.\n");
}
else if(flg == 1)
{
printf("\nStrings are not equal.");
}
return 0;
}
Sample Output:
Check the length of two strings:--------------------------------
Input the 1st string: aabbcc
Input the 2nd string: abcdef
String1: aabbcc
String2: abcdef
Strings are not equal.
Sample Output:
Check the length of two strings:--------------------------------
Input the 1st string: aabbcc
Input the 2nd string: aabbcc
String1: aabbcc
String2: aabbcc
Strings are equal.
Flowchart:
![Flowchart: Compare two strings without using string library functions](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-6.png)
Exercise 7
Write a program in C to count total number of alphabets, digits and special characters in a string.![C Programming: Count total number of alphabets, digits and special characters](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-7.png)
Sample Solution:
C Code:#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define str_size 100 //Declare the maximum size of the string
void main()
{
char str[str_size];
int alp, digit, splch, i;
alp = digit = splch = i = 0;
printf("\n\nCount total number of alphabets, digits and special characters :\n");
printf("--------------------------------------------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
/* Checks each character of string*/
while(str[i]!='\0')
{
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
alp++;
}
else if(str[i]>='0' && str[i]<='9')
{
digit++;
}
else
{
splch++;
}
i++;
}
printf("Number of Alphabets in the string is: %d\n", alp);
printf("Number of Digits in the string is: %d\n", digit);
printf("Number of Special characters in the string is: %d\n\n", splch);
}
Sample Output:
Count total number of alphabets, digits and special characters:--------------------------------------------------------------------
Input the string: Welcome to w3resource.com
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
Flowchart:
![Flowchart: Count total number of alphabets, digits and special characters](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-7d364.png?time=1623438303008)
Exercise 8
Write a program in C to copy one string to another string.![C Programming: Copy one string into another string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-8.png)
Sample Solution:
C Code:#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str1[100], str2[100];
int i;
printf("\n\nCopy one string into another string:\n");
printf("-----------------------------------------\n");
printf("Input the string: ");
fgets(str1, sizeof str1, stdin);
/* Copies string1 to string2 character by character */
i=0;
while(str1[i]!='\0')
{
str2[i] = str1[i];
i++;
}
//Makes sure that the string is NULL terminated
str2[i] = '\0';
printf("\nThe First string is : %s\n", str1);
printf("The Second string is: %s\n", str2);
printf("Number of characters copied: %d\n\n", i);
}
Sample Output:
Copy one string into another string:-----------------------------------------
Input the string: This is a string to be copied.
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
Flowchart:
![Flowchart: Copy one string into another string.](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-8.png)
Exercise 9
Write a program in C to count total number of vowel or consonant in a string.![C Programming: Count total number of vowel or consonant](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-98651.png?time=1623438273712)
Sample Solution:
C Code:#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define str_size 100 //Declare the maximum size of the string
void main()
{
char str[str_size];
int i, len, vowel, cons;
printf("\n\nCount total number of vowel or consonant:\n");
printf("----------------------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
vowel = 0;
cons = 0;
len = strlen(str);
for(i=0; i<len; i++)
{
if(str[i] =='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u' || str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U')
{
vowel++;
}
else if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
cons++;
}
}
printf("\nThe total number of vowel in the string is: %d\n", vowel);
printf("The total number of consonant in the string is: %d\n\n", cons);
}
Sample Output:
Count total number of vowel or consonant:----------------------------------------------
Input the string : Welcome to w3resource.com
The total number of vowel in the string is: 9
The total number of consonant in the string is: 12
Flowchart:
![Flowchart: Count total number of vowel or consonant.](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-91ece.png?time=1623438765311)
Exercise 10
Write a program in C to find maximum occurring character in a string.![C Programming: Find maximum occurring character in a string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-10.png)
Sample Solution:
C Code:#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define str_size 100 //Declare the maximum size of the string
#define chr_no 255 //Maximum number of characters to be allowed
void main()
{
char str[str_size];
int ch_fre[chr_no];
int i = 0, max;
int ascii;
printf("\n\nFind maximum occurring character in a string:\n");
printf("--------------------------------------------------\n");
printf("Input the string : ");
fgets(str, sizeof str, stdin);
for(i=0; i<chr_no; i++) //Set frequency of all characters to 0
{
ch_fre[i] = 0;
}
/* Read for frequency of each characters */
i=0;
while(str[i] != '\0')
{
ascii = (int)str[i];
ch_fre[ascii] += 1;
i++;
}
max = 0;
for(i=0; i<chr_no; i++)
{
if(i!=32)
{
if(ch_fre[i] > ch_fre[max])
max = i;
}
}
printf("The Highest frequency of character '%c' appears number of times: %d \n\n", max, ch_fre[max]);
}
Sample Output:
Find maximum occurring character in a string:--------------------------------------------------
Input the string: Welcome to w3resource
The Highest frequency of character 'e' appears number of times: 4
Flowchart:
![Flowchart: Find maximum occurring character in a string.](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-10.png)
Exercise 11
Write a C program to sort a string array in ascending order.![C Programming: Sort a string array in ascending order](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-110950.png?time=1623438238606)
Sample Solution:
C Code:#include <stdio.h>
#include <string.h>
void main()
{
char str[100],ch;
int i,j,l;
printf("\n\nSort a string array in ascending order:\n");
printf("--------------------------------------------\n");
printf("Input the string : ");
fgets(str, sizeof str, stdin);
l=strlen(str);
/* sorting process */
for(i=1;i<l;i++)
for(j=0;j<l-i;j++)
if(str[j]>str[j+1])
{
ch=str[j];
str[j] = str[j+1];
str[j+1]=ch;
}
printf("After sorting the string appears like: \n");
printf("%s\n\n",str);
}
Sample Output:
Sort a string array in ascending order:--------------------------------------------
Input the string: w3resource
After sorting the string appears like:
3ceeorrsuw
Flowchart :
![Flowchart: Sort a string array in ascending order](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-11.png)
Exercise 12
Write a program in C to read a string through keyboard and sort it using bubble sort.![C Programming: Sorts the strings of an array using bubble sort](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-12385b.png?time=1623438205836)
Sample Solution:
C Code:#include <stdio.h>
#include <string.h>
void main()
{
char name[25][50],temp[25];
int n,i,j;
printf("\n\nSorts the strings of an array using bubble sort :\n");
printf("-----------------------------------------------------\n");
printf("Input number of strings :");
scanf("%d",&n);
printf("Input string %d :\n",n);
for(i=0;i<=n;i++)
{
fgets(name[i], sizeof name, stdin);
}
/*Logic Bubble Sort*/
for(i=1;i<=n;i++)
for(j=0;j<=n-i;j++)
if(strcmp(name[j],name[j+1])>0)
{
strcpy(temp,name[j]);
strcpy(name[j],name[j+1]);
strcpy(name[j+1],temp);
}
printf("The strings appears after sorting:\n");
for(i=0;i<=n;i++)
printf("%s\n",name[i]);
}
Sample Output:
Sorts the strings of an array using bubble sort:-----------------------------------------------------
Input number of strings: 3
Input string 3:
zero
one
two
The strings appears after sorting:
one
two
zero
Flowchart:
![Flowchart: Sorts the strings of an array using bubble sort](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-12.png)
Exercise 13
Write a program in C to extract a substring from a given string.![C Programming: Extract a substring from a given string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-13.png)
Sample Solution:
C Code:#include <stdio.h>
void main()
{
char str[100], sstr[100];
int pos, l, c = 0;
printf("\n\nExtract a substring from a given string:\n");
printf("--------------------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
printf("Input the position to start extraction:");
scanf("%d", &pos);
printf("Input the length of substring:");
scanf("%d", &l);
while (c < l)
{
sstr[c] = str[pos+c-1];
c++;
}
sstr[c] = '\0';
printf("The substring retrieve from the string is: \" %s\ "\n\n", sstr);
}
Sample Output:
Extract a substring from a given string:--------------------------------------------
Input the string: This is test string
Input the position to start extraction: 9
Input the length of substring: 4
The substring retrieve from the string is: " test "
Flowchart:
![Flowchart: Extract a substring from a given string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-13.png)
Exercise 14
Write a C program to check whether a given substring is present in the given string.![C Programming: Check whether a given substring is present in the given string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-140a35.png?time=1623438175467)
Sample Solution:
C Code:#include <stdio.h>
void main()
{
char str[80],search[20];
int c1=0,c2=0,i,j,flg;
printf("\n\nCheck whether a given substring is present in the given string :\n");
printf("-------------------------------------------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
printf("Input the substring to be search: ");
fgets(search, sizeof search, stdin);
while (str[c1]!='\0')
c1++;
c1--;
while (search[c2]!='\0')
c2++;
c2--;
for(i=0;i<=c1-c2;i++)
{
for(j=i;j<i+c2;j++)
{
flg=1;
if (str[j]!=search[j-i])
{
flg=0;
break;
}
}
if (flg==1)
break;
}
if (flg==1)
printf("The substring exists in the string.\n\n");
else
printf("The substring is not exists in the string. \n\n");
}
Sample Output:
Check whether a given substring is present in the given string:-------------------------------------------------------------------
Input the string: This is a test string.
Input the substring to be search: search
The substring is not exists in the string.
Flowchart :
![Flowchart: Check whether a given substring is present in the given string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-140d20.png?time=1623438134879)
Exercise 15
Write a program in C to read a sentence and replace lowercase characters by uppercase and vice-versa.![C Programming: Replace lowercase characters by uppercase and vice-versa](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-15.png)
Sample Solution:
C Code:#include <stdio.h>
#include <string.h>
#include <ctype.h>
void main()
{
char str[100];
int ctr, ch, i;
printf("\n\nReplace lowercase characters by uppercase and vice-versa:\n");
printf("--------------------------------------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
i=strlen(str);
ctr = i; /*shows the number of chars accepted in a sentence*/
printf("\nThe given sentence is: %s",str);
printf("After Case changed the string is: ");
for(i=0; i < ctr; i++)
{
ch = islower(str[i]) ? toupper(str[i]) : tolower(str[i]);
putchar(ch);
}
printf("\n\n");
}
Sample Output:
Replace lowercase characters by uppercase and vice-versa:--------------------------------------------------------------
Input the string: This Is A Test String
The given sentence is: This Is A Test String
After Case changed the string is: tHIS iS a tEST sTRING
Flowchart :
![Flowchart: Replace lowercase characters by uppercase and vice-versa](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-154a42.png?time=1623438109745)
Exercise 16
Write a program in C to find the number of times a given word 'the' appears in the given string.![C Programming: Find the number of times the word 'the' in any combination appears](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-16.png)
Sample Solution:
C Code:#include <stdio.h>
#include <string.h>
void main()
{
int ctr=0,i,freq=0;
int t,h,e,spc;
char str[100];
printf("\n\nFind the number of times the word 'the ' in any combination appears:\n");
printf("----------------------------------------------------------------------\n");
printf("Input the string: ");
fgets(str,sizeof str,stdin);
ctr=strlen(str);
/*Counts the frequency of the word 'the' with a trailing space*/
for(i=0;i<=ctr-3;i++)
{
t=(str[i]=='t'||str[i]=='T');
h=(str[i+1]=='h'||str[i+1]=='H');
e=(str[i+2]=='e'||str[i+2]=='E');
spc=(str[i+3]==' '||str[i+3]=='\0');
if ((t&&h&&e&&spc)==1)
freq++;
}
printf("The frequency of the word \'the\' is: %d\n\n",freq);
}
Sample Output:
Find the number of times the word 'the ' in any combination appears:----------------------------------------------------------------------
Input the string: The stering where the word the present more then onces.
The frequency of the word 'the' is: 3
Flowchart:
![Flowchart: Find the number of times the word 'the ' in any combination appears](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-16fdbd.png?time=1623438078006)
Exercise 17
Write a program in C to remove characters in String Except Alphabets.![C Programming: Remove characters in String Except Alphabets](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-1747d3.png?time=1623438015925)
Sample Solution:
C Code:#include <stdio.h>
#include <string.h>
void main(){
char str[150];
int i,j;
printf("\n\nRemove characters in String Except Alphabets:\n");
printf("--------------------------------------------------\n");
printf("Input the string : ");
fgets(str,sizeof str,stdin);
for(i=0; str[i]!='\0'; ++i)
{
while (!((str[i]>='a'&&str[i]<='z') || (str[i]>='A'&&str[i]<='Z' || str[i]=='\0')))
{
for(j=i;str[j]!='\0';++j)
{
str[j]=str[j+1];
}
str[j]='\0';
}
}
printf("After removing the Output String: %s\n\n",str);
}
Sample Output:
Remove characters in String Except Alphabets :--------------------------------------------------
Input the string: W3resource.com
After removing the Output String: Wresourcecom
Flowchart:
![Flowchart: Remove characters in String Except Alphabets](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-17.png)
Exercise 18
Write a program in C to Find the Frequency of Characters.![C Programming: Find the Frequency of Characters](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-18.png)
Sample Solution:
C Code:#include <stdio.h>
void main(){
char str[1000],choice;
int i,ctr=0;
printf("\n\nFind the Frequency of Characters:\n");
printf("--------------------------------------\n");
printf("Input the string : ");
fgets(str,sizeof str,stdin);
printf("Input the character to find frequency: ");
scanf("%c",&choice);
for(i=0;str[i]!='\0';++i)
{
if(choice==str[i])
++ctr;
}
printf("The frequency of '%c' is: %d\n\n", choice, ctr);
}
Sample Output:
Find the Frequency of Characters:--------------------------------------
Input the string: This is a test string
Input the character to find frequency: i
The frequency of 'i' is: 3
Flowchart:
![Flowchart: Find the Frequency of Characters](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-18.png)
Exercise 19
Write a program in C to Concatenate Two Strings Manually.![C Programming: Concatenate Two Strings Manually](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-19.png)
Sample Solution:
C Code:#include <stdio.h>
#include <string.h>
void main()
{
char str1[100], str2[100], i, j,l,m,k;
printf("\n\nConcatenate Two Strings Manually:\n");
printf("-------------------------------------\n");
printf("Input the first string: ");
fgets(str1,sizeof str1,stdin);
printf("Input the second string: ");
fgets(str2,sizeof str2,stdin);
l=strlen(str1);
m=strlen(str2);
for(i=0; i<l-1; ++i); /* value i contains reaches the end of string str1. */
str1[i]=' '; /* add a space with string str1. */
i++; /* value i increase by 1 for the blank space */
for(j=0; j<m-1; ++j, ++i)
{
str1[i]=str2[j];
}
k=strlen(str1);
printf("After concatenation the string is: \n ");
for(i=0; i<k; ++i)
{
printf("%c",str1[i]);
}
printf("\n\n");
}
Sample Output:
Concatenate Two Strings Manually:
-------------------------------------
Input the first string: this is string one
Input the second string: this is string two
After concatenation the string is:
this is string one this is string two
Flowchart:
Exercise 20
Write a program in C to find the largest and smallest word in a string.![C Programming: Find the largest and smallest word in a string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-20.png)
Sample Solution:
C Code:#include <stdio.h>
#include <string.h>
#include <ctype.h>
void main()
{
char str[100], word[20], mx[20], mn[20], c;
int i = 0, j = 0, flg = 0;
printf("\n\nFind the largest and smallest word in a string:\n");
printf("-----------------------------------------------------\n");
printf("Input the string : ");
i = 0;
do
{
fflush(stdin);
c = getchar();
str[i++] = c;
} while (c != '\n');
str[i - 1] = '\0';
for (i = 0; i < strlen(str); i++)
{
while (i < strlen(str) && !isspace(str[i]) && isalnum(str[i]))
{
word[j++] = str[i++];
}
if (j != 0)
{
word[j] = '\0';
if (!flg)
{
flg = !flg;
strcpy(mx, word);
strcpy(mn, word);
}
if (strlen(word) > strlen(mx))
{
strcpy(mx, word);
}
if (strlen(word) < strlen(mn))
{
strcpy(mn, word);
}
j = 0;
}
}
printf("The largest word is '%s' \nand the smallest word is '%s' \nin the string: '%s'.\n", mx, mn, str);
}
Sample Output:
Find the largest and smallest word in a string:-----------------------------------------------------
Input the string: It is a string with smallest and largest word.
The largest word is 'smallest'
and the smallest word is 'a'
in the string: 'It is a string with smallest and largest word'.
Flowchart:
![Flowchart: Find the largest and smallest word in a string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-20.png)
Exercise 21
Write a program in C to convert a string to uppercase.![C Programming: Convert a string to uppercase](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-21.png)
Sample Solution:
C Code:#include<stdio.h>
#include<ctype.h>
int main()
{
int ctr=0;
char str_char;
char str[100];
printf("\n Convert a string to uppercase:\n");
printf("-----------------------------------");
printf("\n Input a string in lowercase: ");
fgets(str, sizeof str, stdin);
printf(" Here is the above string in UPPERCASE:\n ");
while (str[ctr])
{
str_char=str[ctr];
putchar (toupper(str_char));
ctr++;
}
printf("\n\n");
return 0;
}
Sample Output:
Convert a string to uppercase:
-----------------------------------
Input a string in lowercase: the quick brown fox jumps over the lazy dog.
Here is the above string in UPPERCASE:
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
Flowchart:
![Flowchart: Convert a string to uppercase](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-21.png)
Exercise 22
Write a program in C to convert a string to lowercase.![C Programming: Convert a string to lowercase](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-22.png)
Sample Solution:
C Code:#include<stdio.h>
#include<ctype.h>
int main()
{
int ctr=0;
char str_char;
char str[100];
printf("\n Convert a string to lowercase:\n");
printf("----------------------------------");
printf("\n Input a string in UPPERCASE: ");
fgets(str, sizeof str, stdin);
printf(" Here is the above string in lowercase:\n ");
while (str[ctr])
{
str_char=str[ctr];
putchar (tolower(str_char));
ctr++;
}
return 0;
}
Sample Output:
Convert a string to lowercase:----------------------------------
Input a string in UPPERCASE: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
Here is the above string in lowercase:
the quick brown fox jumps over the lazy dog.
Flowchart:
![Flowchart: Convert a string to lowercase](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-22.png)
Exercise 23
Write a program in C to check whether a character is Hexadecimal Digit or not.![C Programming: Check whether a character is Hexadecimal Digit or not](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-23.png)
Sample Solution:
C Code:#include<stdio.h>
#include<ctype.h>
int main()
{
char TestChar;
printf("\n Check whether a character is Hexadecimal Digit or not:\n");
printf("-------------------------------------------------------\n");
printf(" Input a character: "); // Hexadecimal Digits are a-f, A-F or 0-9
scanf( "%c", &TestChar );
if( isxdigit(TestChar) )
printf( " The entered character is a hexadecimal digit. \n" );
else
printf( " The entered character is not a hexadecimal digit. \n" );
return 0;
}
Sample Output:
Check
whether a character is Hexadecimal Digit or not:-------------------------------------------------------
Input a character: 7
The entered character is a hexadecimal digit.
Flowchart:
![Flowchart: Check whether a character is Hexadecimal Digit or not](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-23.png)
Exercise 24
Write a program in C to check whether a letter is uppercase or not.![C Programming: Check whether a letter is uppercase or not](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-24.png)
Sample Solution:
C Code:#include<stdio.h>
#include<ctype.h>
int main()
{
char TestChar;
printf("\n Check whether a letter is uppercase or not:\n");
printf("------------------------------------------------\n");
printf(" Input a character: ");
scanf( "%c", &TestChar );
if( isupper(TestChar) )
printf( " The entered letter is an UPPERCASE letter. \n" );
else
printf( " The entered letter is not an UPPERCASE letter. \n" );
return 0;
}
Sample Output:
Check whether a letter is uppercase or not:------------------------------------------------
Input a character: p
The entered letter is not an UPPERCASE letter.
Flowchart :
![Flowchart: Check whether a letter is uppercase or not](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-24.png)
Exercise 25
Write a program in C to replace the spaces of a string with a specific character.![C Programming: Replace the spaces of a string with a specific character](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-25.png)
Sample Solution:
C Code:#include<stdio.h>
#include<ctype.h>
int main()
{
int new_char;
char t;
int ctr=0;
char str[100];
printf("\n Replace the spaces of a string with a specific character:\n");
printf("-------------------------------------------------------------\n");
printf(" Input a string: ");
fgets(str, sizeof str, stdin);
printf(" Input replace character: ");
scanf("%c",&t);
printf(" After replacing the space with %c the new string is:\n",t);
while (str[ctr])
{
new_char=str[ctr];
if (isspace(new_char))
new_char=t;
putchar (new_char);
ctr++;
}
printf("\n\n");
return 0;
}
Sample Output:
Replace the spaces of a string with a specific character:-------------------------------------------------------------
Input a string: Be glad to see the back of
Input replace character: *
After replacing the space with * the new string is:
Be*glad*to*see*the*back*of*
Flowchart:
![Flowchart: Replace the spaces of a string with a specific character](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-25.png)
Exercise 26
Write a program in C to count the number of punctuation characters exists in a string.![C Programming: Count the number of punctuation characters exists in a string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-26.png)
Sample Solution:
C Code:#include<stdio.h>
#include<ctype.h>
int main()
{
int ctr1=0;
int ctr2=0;
char str[100];
printf("\n Count the number of punctuation characters exists in a string:\n");
printf("------------------------------------------------------------------\n");
printf(" Input a string: ");
fgets(str, sizeof str, stdin);
while (str[ctr1])
{
if (ispunct(str[ctr1])) ctr2++;
ctr1++;
}
printf (" The punctuation characters exists in the string is: %d\n\n", ctr2);
return 0;
}
Sample Output:
Count the number of punctuation characters exists in a string:------------------------------------------------------------------
Input a string: The quick brown fox,jumps over the,lazy dog.
The punctuation characters exists in the string is: 3
Flowchart:
![Flowchart: Count the number of punctuation characters exists in a string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-26.png)
Exercise 27
Write a program in C to print only the string before new line character.Note: isprint() will only print line one, because the newline character is not printable.
Sample Solution:
C Code:#include<stdio.h>
#include<ctype.h>
int main()
{
int ctr=0;
char str[]=" The quick brown fox \n jumps over the \n lazy dog. \n";
printf("\n Print only the string before new line character:\n");
printf("----------------------------------------------------\n");
while (isprint(str[ctr]))
{
putchar (str[ctr]);
ctr++;
}
printf("\n\n");
return 0;
}
Sample Output:
Print only the string before new line character:----------------------------------------------------
The quick brown fox
Flowchart:
![Flowchart: Print only the string before new line character](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-27.png)
Exercise 28
Write a program in C to check whether a letter is lowercase or not.![C Programming: Check whether a letter is lowercase or not](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-28.png)
Sample Solution:
C Code:#include<stdio.h>
#include<ctype.h>
int main()
{
char TestChar;
printf("\n Check whether a letter is lowercase or not:\n");
printf("------------------------------------------------\n");
printf(" Input a character: ");
scanf( "%c", &TestChar );
if( islower(TestChar) )
printf( " The entered letter is a lowercase letter. \n" );
else
printf( " The entered letter is not a lowercase letter. \n" );
return 0;
}
Sample Output:
Check whether a letter is lowercase
or not:------------------------------------------------
Input a character: w
The entered letter is a lowercase letter.
Flowchart:
![Flowchart: Check whether a letter is lowercase or not](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-28.png)
Exercise 29
Write a program in C to read a file and remove the spaces between two words of its content.![C Programming: Remove the spaces between two words of its content](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-29.png)
Sample Solution:
C Code:#include<stdio.h>
#include<ctype.h>
int main()
{
FILE * pfile;
int a;
printf("\n Remove the spaces between two words:\n");
printf("-----------------------------------------\n");
// file.txt contain : the quick brown fox jumps over the lazy dog
pfile=fopen ("file.txt","r");
printf(" The content of the file is :\n The quick brown fox jumps over the lazy dog\n\n");
printf(" After removing the spaces the content is: \n");
if (pfile)
{
do {
a = fgetc (pfile);
if ( isgraph(a) ) putchar (a);
} while (a != EOF);
fclose (pfile);
}
printf("\n\n");
return 0;
}
Sample Output:
Remove the spaces between two words:-----------------------------------------
The content of the file is:
The quick brown fox jumps over the lazy dog
After removing the spaces the content is:
Thequickbrownfoxjumpsoverthelazydog
Flowchart:
![Flowchart: Remove the spaces between two words of its content](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-29.png)
Exercise 30
Write a program in C to check whether a character is digit or not.![C Programming: Check whether a character is digit or not](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-30.png)
Sample Solution:
C Code:#include<stdio.h>
#include<ctype.h>
int main()
{
char TestChar;
printf("\n Check whether a character is digit or not:\n");
printf("----------------------------------------------\n");
printf(" Input a character: ");
scanf( "%c", &TestChar );
if( isdigit(TestChar) )
printf( " The entered character is a digit. \n\n" );
else
printf( " The entered character is not a digit. \n\n" );
return 0;
}
Sample Output:
Check whether a character is digit or not:----------------------------------------------
Input a character: 8
The entered character is a digit.
Flowchart:
![Flowchart: Check whether a character is digit or not](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-30.png)
Exercise 31
Write a program in C to split string by space into words.![C Programming: Split string by space into words](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-image-exercise-31.png)
Sample Solution:
C Code:#include <stdio.h>
#include <string.h>
int main()
{
char str1[100];
char newString[10][10];
int i,j,ctr;
printf("\n\n Split string by space into words:\n");
printf("---------------------------------------\n");
printf(" Input a string: ");
fgets(str1, sizeof str1, stdin);
j=0; ctr=0;
for(i=0;i<=(strlen(str1));i++)
{
// if space or NULL found, assign NULL into newString[ctr]
if(str1[i]==' '||str1[i]=='\0')
{
newString[ctr][j]='\0';
ctr++; //for next word
j=0; //for next word, init index to 0
}
else
{
newString[ctr][j]=str1[i];
j++;
}
}
printf("\n Strings or words after split by space are:\n");
for(i=0;i < ctr;i++)
printf(" %s\n",newString[i]);
return 0;
}
Sample Output:
Split string by space into words:---------------------------------------
Input a string: this is a test string
Strings or words after split by space are:
this
is
a
test
string
Flowchart:
![Flowchart: Split string by space into words](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-31.png)
Exercise 32
Write a C programming to find the repeated character in a given string.Sample Solution:
C Code:#include<stdio.h>
#include
int ifexists(char p, char q[], int v)
{
int i;
for (i=0; i<v;i++)
if (q[i]==p) return (1);
return (0);
}
int main()
{
char string1[80],string2[80];
int n,i,x;
printf("Input a string: ");
scanf("%s",string1);
n=strlen(string1);
string2[0]=string1[0];
x=1;
for(i=1;i < n; i++)
{
if(ifexists(string1[i], string2, x))
{
printf("The first repetitive character in %s is: %c ", string1, string1[i]);
break;
}
else
{
string2[x]=string1[i];
x++;
}
}
if(i==n)
printf("There is no repetitive character in the string %s.", string1);
}
Sample Output:
Input a string: The first repetitive character in w3resource is: rFlowchart:
![Flowchart: Find the repeated character in a given string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-32.png)
Exercise 33
Write a C programming to count of each character in a given string.Sample Solution:
C Code:#include<stdio.h>
#include<string.h>
int if_char_exists(char c, char p[], int x, int y[])
{
int i;
for (i=0; i<=x;i++)
{
if (p[i]==c)
{
y[i]++;
return (1);
}
}
if(i>x) return (0);
}
int main()
{
char str1[80],chr[80];
int n,i,x,ctr[80];
printf("Enter a str1ing: ");
scanf("%s",str1);
n=strlen(str1);
chr[0]=str1[0];
ctr[0]=1;
x=0;
for(i=1;i < n; i++)
{
if(!if_char_exists(str1[i], chr, x, ctr))
{
x++;
chr[x]=str1[i];
ctr[x]=1;
}
}
printf("The count of each character in the string %s is \n", str1);
for (i=0;i<=x;i++)
printf("%c\t%d\n",chr[i],ctr[i]);
}
Sample Output:
Enter a str1ing: The count of each character in the string w3resource isw 1
3 1
r 2
e 2
s 1
o 1
u 1
c 1
Flowchart:
![Flowchart: Count of each character in a given string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-33.png)
Exercise 34
Write a C programming to convert vowels into upper case character in a given string.Sample Solution:
C Code:#include
int main()
{
char string1[255];
int i;
printf("Input a sentence: ");
gets(string1);
printf("The original string:\n");
puts(string1);
i=0;
while(string1[i]!='\0')
{
if(string1[i]=='a' ||string1[i]=='e' ||string1[i]=='i' ||string1[i]=='o' ||string1[i]=='u')
string1[i]=string1[i]-32;
i++;
}
printf("After converting vowels into upper case the sentence becomes:\n");
puts(string1);
}
Sample Solution:
Input a sentence: The original string:w3resource
After converting vowels into upper case the sentence becomes:
w3rEsOUrcE
Flowchart:
![Flowchart: Convert vowels into upper case character in a given string](../../../../pluginfile.php/3216421/mod_book/chapter/15222/c-string-exercise-flowchart-34b692.png?time=1623437183863)