0% found this document useful (0 votes)
3 views

Unit 3 String Excercise 4

Uploaded by

tv5874
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit 3 String Excercise 4

Uploaded by

tv5874
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

School of Computing Science and Engineering

Course Code : R1UC101B Name: Programming for Problem Solving-C

UNIT III
String: Exercise:-4

Program Name: B.Tech. (CSE)


Ex-1 : C Program to Remove all Characters in a String Except
Alphabets
In this example, you will learn to remove all the characters from a string
entered by the user except the alphabets.
#include <stdio.h>
int main() {
char line[150];

printf("Enter a string: ");


fgets(line, sizeof(line), stdin); // take input

for (int i = 0, j; line[i] != '\0'; ++i) {


Continue…

// enter the loop if the character is not an alphabet


// and not the null character
while (!(line[i] >= 'a' && line[i] <= 'z') && !(line[i] >= 'A' && line[i] <=
'Z') && !(line[i] == '\0')) {
for (j = i; line[j] != '\0'; ++j) {

// if jth element of line is not an alphabet,


// assign the value of (j+1)th element to the jth element
line[j] = line[j + 1];
}
line[j] = '\0';
}
}
Continue…

printf("Output String: ");


puts(line);
return 0;
}
Output
Enter a string: p2'r-o@gram84iz./
Output String: programiz

This program takes a string input from the user and stores in the line
variable. Then, a for loop is used to iterate over characters of the
string.
If the character in a string is not an alphabet, it is removed from the
string and the position of the remaining characters are shifted to the
left by 1 position.
References

 http://kirste.userpage.fu-berlin.de/chemnet/use/info/libc/libc_7.html
 Let Us C by Yashavant Kanetkar : Authentic Guide to C PROGRAMMING Language 17th
Edition, BPB Publications
 C in Depth by by S.K.Srivastava and Deepali Srivastava, BPB Publications

You might also like