148k views
0 votes
Write a program to accept a one-line string (maximum of 100 characters) from the keyboard. Edit the string entered in Part 1a (with code that you write) to remove all the white space,digits, punctuation, and other special characters, leaving only the letters. Print out the resulting compressed string to the screen.

2 Answers

1 vote

Answer:

w = str(input("input your values: "))

values = ' '.join(filter(str.isalpha, w))

while len(w) < 100:

print(values)

break

Step-by-step explanation:

The code is written in python

w = str(input("input your values: "))

This code ask the user to input any string values with characters, numbers, line spaces , letters etc.

values = ' '.join(filter(str.isalpha, w))

This code filters the inputted value to bring only letters. All the letter are then joined together

while len(w) < 100:

The code check if the inputted value is less than 100 characters. While it is less than 100 characters. If it is less than 100 character the next code will function.

print(values)

This code prints the joined letters after checking with a while loop to confirm the length of character is less than 100

break

The break function breaks the code whether it print the values or not.

Generally, the letters will only be printed if the character inputted is less than 100 and later break the while loop or will not print any letter if the character is greater than 100 and later break.

User Webdizz
by
3.6k points
6 votes

Answer:

// This program is written in C++ programming language

// Comments are used for explanatory purpose

/* The aim of this program is to to remove all the white space,digits, punctuation, and other special characters, leaving only the letters. */

// Program starts here

#include <stdio.h>

#include<iostream>

using namespace std;

int main()

{

// Declare Variable of 100 characters

char word[100];

// Prompt user for input

cout<<"Your input goes here (max, 100 characters)";

cin>>word;

// Iterate through string to check for non alphabetic characters

for (int i = 0; word[i] != '\0'; ++i) {

// Check for uppercase and lowercase letters

while (!((word[i] >= 'a' && word[i] <= 'z') || (word[i] >= 'A' && word[i] <= 'Z') || word[i] == '\0')) {

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

word[j] = word[j + 1];

}

word[j] = '\0';

}

}

cout<<"The resulting compressed string: "<<word;

return 0;

}

User Xiaoye
by
3.4k points