- Write the function isAlphabet that checks if a character passed as parameter is an alphabet or not (the function returns 1 if the character is an alphabet or 0 otherwise).
- Write the function EliminationOfNoAlphabet that takes a string S as parameter and eliminates the non-alphabet characters from it.
Example: if S = "ab A 132?CD#" after elimination, the string S should become "abACD" - Write a function that takes two strings S1 and S2 as parameters, and regroups all the alphabets of both strings (S1 and S2) in S1, and the other characters of both strings in S2.
Example: if S1 = "ab123??” and S2 = "#@Mp9C"
After calling the function they should become: S1="abMpC" and S2 = "123??#@9"
Hint : You can use the function strcpy(STR1, STR2) that copies string STR2 into string STR1.
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<string.h>
#define SIZE 100
int isAlphabet(char c)
{
return c>='a' && c<='z' || c>='A' && c<='Z' ;
}
void EliminationOfNoAlphabet(char str[])
{
int i ,j=0;
for(i=0;str[i];i++)
if(isAlphabet(str[i]))
str[j++]=str[i];
str[j]='\0';
}
void regroup(char s1[], char s2[])
{
int i ,j=0;
char s1copy[SIZE], s2copy[SIZE] ;
strcpy(s1copy,s1);
strcpy(s2copy,s2);
for(i=0;s1[i];i++)
if(isAlphabet(s1[i]))
s1[j++]=s1[i];
for(i=0;s2[i];i++)
if(isAlphabet(s2[i]))
s1[j++]=s2[i];
s1[j]='\0';
j=0;
for(i=0;s1copy[i];i++)
if(!isAlphabet(s1copy[i]))
s2[j++]=s1copy[i];
for(i=0;s2copy[i];i++)
if(!isAlphabet(s2copy[i]))
s2[j++]=s2copy[i];
s2[j]='\0';
}
void main()
{
char s1[SIZE], s2[SIZE];
printf("Enter a string: ");
scanf("%s",s1);
printf("Enter a string: ");
scanf("%s",s2);
regroup(s1, s2);
printf("S1 = \"%s\"\n",s1);
printf("S2 = \"%s\"\n",s2);
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Set of integer in binary search trees