Write a c++ trim function that takes an input of string and output the string by trimming away all non-alphabetic letters.
So here is the program. Save it in file trimming.cpp
#include <iostream>
using namespace std;
// Function to remove non albhabetic characters from a given
string
void removeNonAlpbabetic(char * str) {
// counter to hold albhabetic character
int count = 0;
// Loop through the given string. If current character
// is an alphabet, place it at index 'count++'
for (int i = 0; str[i]; i++) {
if (isalpha(str[i])) {
// assign the albhabetis character in currentposition and increment
the counter
str[count++] = str[i];
}
}
// After the loop ends, put the string termination character to
mark the end of string.
str[count] = '\0';
}
int main() {
char str[] = "To Test 9 trimming 2# + of non albhabetic
characters!!!";
removeNonAlpbabetic(str);
cout << str;
return 0;
}
Sample output :
ToTesttrimmingofnonalbhabeticcharacters
Get Answers For Free
Most questions answered within 1 hours.