String Exercises - Solutions
String Exercises - Solutions
String Exercises - Solutions
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");
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:
Help
Exercise 2
Write a program in C to find the length of a string without using library function.
Sample Solution:
C Code:
#include <stdio.h>
#include <stdlib.h>
void main()
{
char str[100]; /* Declares a string of size 100 */
int l= 0;
Sample Output:
Find the length of a string:
---------------------------------
Input the string: w3resource.com
Length of the string is: 15
Flowchart:
Exercise 3
Write a program in C to separate the individual characters from a string.
Sample Solution:
C Code:
#include <stdio.h>
#include <stdlib.h>
void main()
{
char str[100]; /* Declares a string of size 100 */
int l= 0;
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:
Exercise 4
Write a program in C to print individual characters of string in reverse order.
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;
for(i=l;i>=0;i--)
{
printf("%c ", str[i]);
}
printf("\n");
}
#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");
}
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 :
Exercise 5
Write a program in C to count the total number of words in a string.
Sample Solution:
C Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str[str_size];
int i, wrd;
i = 0;
wrd = 1;
i++;
}
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:
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 != *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);
Sample Output:
Check the length of two strings:
--------------------------------
Input the 1st string: aabbcc
Input the 2nd string: abcdef
String1: aabbcc
String2: abcdef
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:
Exercise 7
Write a program in C to count total number of alphabets, digits and special characters in a string.
Sample Solution:
C Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str[str_size];
printf("--------------------------------------------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
while(str[i]!='\0')
{
if((str[i]>='a' && str[i]<='z') || (str[i]>='A'
&& str[i]<='Z'))
{
alp++;
}
else
{
splch++;
}
i++;
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:
Exercise 8
Write a program in C to copy one string to another string.
Sample Solution:
C Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str1[100], str2[100];
int i;
str2[i] = str1[i];
i++;
}
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.
Flowchart:
Exercise 9
Write a program in C to count total number of vowel or consonant in a string.
Sample Solution:
C Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str[str_size];
vowel = 0;
cons =
0;
len = strlen(str);
}
}
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
Exercise 10
Write a program in C to find maximum occurring character in a string.
Sample Solution:
C Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str[str_size];
int ch_fre[chr_no];
int i = 0, max;
int ascii;
ch_fre[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:
Exercise 11
Write a C program to sort a string array in ascending order.
Sample Solution:
C Code:
#include <stdio.h>
#include <string.h>
void
main()
{
char str[100],ch;
int i,j,l;
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;
}
Sample Output:
Sort a string array in ascending order:
--------------------------------------------
Input the string: w3resource
After sorting the string appears like:
3ceeorrsuw
Flowchart :
Exercise 12
Write a program in C to read a string through keyboard and sort it using bubble sort.
Sample Solution:
C Code:
#include <stdio.h>
#include
<string.h>
void main()
{
char name[25][50],temp[25];
int n,i,j;
printf("-----------------------------------------------------\n");
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:
Exercise 13
Write a program in C to extract a substring from a given string.
Sample Solution:
C Code:
#include <stdio.h>
void main()
{
scanf("%d", &pos);
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:
Exercise 14
Write a C program to check whether a given substring is present in the given string.
Sample Solution:
C Code:
#include <stdio.h>
void main()
{
char str[80],search[20];
int c1=0,c2=0,i,j,flg;
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 :
Exercise 15
Write a program in C to read a sentence and replace lowercase characters by uppercase and vice-versa.
Sample Solution:
C Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void main()
{
char str[100];
int ctr, ch, i;
i=strlen(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
Flowchart :
Exercise 16
Write a program in C to find the number of times a given word 'the' appears in the given string.
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");
ctr=strlen(str);
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++;
}
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:
Exercise 17
Write a program in C to remove characters in String Except Alphabets.
Sample Solution:
C Code:
#include <stdio.h>
#include <string.h>
void
main(){
char str[150];
int i,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:
Exercise 18
Write a program in C to Find the Frequency of Characters.
Sample Solution:
C Code:
#include <stdio.h>
void main(){
char str[1000],choice;
int i,ctr=0;
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
Exercise 19
Write a program in C to Concatenate Two Strings Manually.
Sample Solution:
C Code:
#include <stdio.h>
#include <string.h>
void main()
{
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);
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.
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("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(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:
Exercise 21
Write a program in C to convert a string to uppercase.
Sample Solution:
C Code:
#include<stdio.h>
#include<ctype.h>
int main()
{
int ctr=0;
char str_char;
char str[100];
{
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:
Exercise 22
Write a program in C to convert a string to lowercase.
Sample Solution:
C Code:
#include<stdio.h>
#include<ctype.h>
int main()
{
int ctr=0;
char str_char;
char str[100];
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:
Exercise 23
Write a program in C to check whether a character is Hexadecimal Digit or not.
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:
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
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");
Sample Output:
Check whether a letter is uppercase or not:
------------------------------------------------
Input a character: p
The entered letter is not an UPPERCASE letter.
Flowchart :
Exercise 25
Write a program in C to replace the spaces of a string with a specific character.
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);
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:
Exercise 26
Write a program in C to count the number of punctuation characters exists in a string.
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
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:
Exercise 28
Write a program in C to check whether a letter is lowercase or not.
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");
Sample Output:
Check whether a letter is lowercase or not:
------------------------------------------------
Input a character: w
The entered letter is a lowercase letter.
Flowchart:
Exercise 29
Write a program in C to read a file and remove the spaces between two words of its content.
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("\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
Flowchart:
Exercise 30
Write a program in C to check whether a character is digit or not.
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");
Sample Output:
Check whether a character is digit or not:
----------------------------------------------
Input a character: 8
The entered character is a digit.
Flowchart:
Exercise 31
Write a program in C to split string by space into words.
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");
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
Flowchart:
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)
Sample Output:
Input a string: The first repetitive character in w3resource is: r
Flowchart:
Flowchart: Find the repeated character in a given string
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;
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 is
w 1
3 1
r 2
e 2
s 1
o 1
u 1
c 1
Flowchart:
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: