The integer bounds you gave don't match the ASCII codes. For example, 'H' is 72.

As the commenters suggest, instead of reading up on the ASCII table, you should use char literals. So,

if ( ( inputString[i] >= 'A' && inputString[i] <= 'Z' )
        || ( inputString[i] >= 'a' && inputString[i] <= 'z' )
        || ( inputString[i] >= '0' && inputString[i] <= '9' ) ) {

You could also avoid all of this by using isalnum from ctype.h.

Answer from Daniel Walker on Stack Overflow
Discussions

How to erase all non alphanumeric characters from a string?
But it erases all the characters from where it finds a non alphanumeric character. If I input: it. is? awesome! It Prints: it But it should print: itisawesome ... Your post specifies alphanumeric. If this is really what you want, you should use isalnum instead of isalpha ... Topic archived. No new replies allowed. Home page | Privacy policy © cplusplus... More on cplusplus.com
🌐 cplusplus.com
How to strip all non alphanumeric characters from a string in c++? - Stack Overflow
Write a function that takes a char ... want to remove that character or false if you want to keep it: ... Depending on your requirements, you may be able to use one of the Standard Library predicates, like std::isalnum, instead of writing your own predicate (you said you needed to match alphanumeric characters and spaces, so perhaps this doesn't exactly fit what you need). If you want to use the Standard Library std::isalnum function, you will need a cast to disambiguate ... More on stackoverflow.com
🌐 stackoverflow.com
string - replace all NON alpha numeric w - C++ Forum
I have a near identical function ... all nonalnum including spaces. Self explanatory what I want this function to do. For some reason it never enters the isalnum test whereas the erase one does. Really confused... ... You know you can assign to strings like they are arrays, right? No reason to call .replace for single characters... More on cplusplus.com
🌐 cplusplus.com
How do I remove all characters that are not numbers or letters from a character array
You wrote you could only use and . std::string has an erase function to remove one or more characters. So what are the actual requirements? Can you use std::string, can you use std::algorithm? In C++ you would write something like, std::string instr; std::cin >> instr; auto new_end = std::remove_if(std::begin(instr), std::end(instr), [](auto c) { return !std::isalnum(c);}); instr.erase(new_end, std::end(instr)); You could do this with character arrays, just that there's no erase, you would just set *new_end to 0 to mark the new end. Question is, what are your actual constraints? More on reddit.com
🌐 r/cpp_questions
53
9
October 10, 2021
🌐
Vultr
docs.vultr.com › clang › examples › remove-all-characters-in-a-string-except-alphabets
C Program to Remove all Characters in a String Except Alphabets | Vultr Docs
September 27, 2024 - This code defines the filterAlphabets function which iterates over each character in the string str and checks if the character is an alphabet. If true, it stores it in the result string. The resultant string contains only alphabetic characters, which is then printed. Modify the function to alter the string in place. Utilize two pointers approach to manage the removal efficiently.
🌐
Cplusplus
cplusplus.com › forum › beginner › 148876
How to erase all non alphanumeric characters from a string?
But it erases all the characters from where it finds a non alphanumeric character. If I input: it. is? awesome! It Prints: it But it should print: itisawesome ... Your post specifies alphanumeric. If this is really what you want, you should use isalnum instead of isalpha ... Topic archived. No new replies allowed. Home page | Privacy policy © cplusplus...
🌐
YouTube
youtube.com › watch
Remove Non Alphabet Characters A From String | C Programming Example - YouTube
How to remove the non-alphabetic characters from a string using C. Source code: https://github.com/portfoliocourses/c-example-code/blob/main/remove_non_alph...
Published   June 7, 2023
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › remove-characters-alphabets-string
Remove all characters other than alphabets from string - GeeksforGeeks
Iterate a for loop on string, if the character is in la or ua using in and not in operators concatenate them to an empty string. Display the string after the end of the loop. ... // C++ program to remove all the // characters other than alphabets #include<bits/stdc++.h> using namespace std; string removeSpecialCharacter(string s) { string t = ""; string la = "abcdefghijklmnopqrstuvwxyz"; string ua = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for (int i = 0; i < s.length(); i++) { if(la.find(s[i]) != string::npos || ua.find(s[i]) != string::npos) { t += s[i]; } } cout << t << endl; return t; } int main() { string s = "$Gee*k;s..fo, r'Ge^eks?"; removeSpecialCharacter(s); return 0; } // This code is contributed by shivamsharma215
Published   July 31, 2023
Find elsewhere
🌐
Quora
quora.com › How-can-I-remove-non-alphanum-characters-in-a-text-file-by-reading-them-into-and-string-and-how-can-I-send-the-non-alphanum-removed-string-to-a-linked-list-in-C
How to remove non-alphanum characters in a text file by reading them into and string and how can I send the non-alphanum-removed string to a linked list in C++ - Quora
Answer: I am going to assume you can figure out the code/syntax on your own, because this smells like homework. Here is a general algoright you can use: 1. Create a stream pointing to the file. 2. Read each character until stream is empty (while loop) 3. 1. For each character use a function to ...
🌐
Progresstalk
progresstalk.com › home › forums › development › development
Removing Non-Alpha Characters from String | ProgressTalk.com
July 18, 2014 - /* or... a bit less obvious */ function xAlphaNum returns character ( input c as character ): return ( if (( c ge "a" and c le "z" ) or ( c ge "0" and c le "9" )) then c else "" ). end. n = "". do i = 1 to length( s ): n = n + xAlphaNum( substring( s, i, 1 )). end. display n. ... Note that the problem "Remove any character which is not alpha" is a somewhat different problem than "Remove any one of the following non-alpha characters that might appear in the string".
🌐
PREP INSTA
prepinsta.com › home › c program › c program to remove all characters from string except alphabets
Removing all characters from string except alphabets | PrepInsta
October 13, 2022 - We will iterate this step through a for loop until every non alphabet character is removed. ... #include #include int main() { int i,j=0,l; char str[100],cpy[100]; printf(“string?”); scanf(“%s”,&str); l=strlen(str); for(i=0;i=’a’&&str[i]=’A’&&str[i]<='Z') { cpy[j]=str[i]; j++; } }
🌐
Altair
help.altair.com › 2022.1 › monarch › help › desktop › Remove_Non_Alphanumeric_Characters.htm
Altair Monarch 2022.1 Help File - Remove Non Alphanumeric Characters
Removes all characters that are not numbers (0-9) or letters of the alphabet (A-Z/a-z) from a column value. In this example, Description is the original column, and Description Clean is the new column after Remove Non Alphanumeric Characters is applied:
Top answer
1 of 12
53

Write a function that takes a char and returns true if you want to remove that character or false if you want to keep it:

bool my_predicate(char c);

Then use the std::remove_if algorithm to remove the unwanted characters from the string:

std::string s = "my data";
s.erase(std::remove_if(s.begin(), s.end(), my_predicate), s.end());

Depending on your requirements, you may be able to use one of the Standard Library predicates, like std::isalnum, instead of writing your own predicate (you said you needed to match alphanumeric characters and spaces, so perhaps this doesn't exactly fit what you need).

If you want to use the Standard Library std::isalnum function, you will need a cast to disambiguate between the std::isalnum function in the C Standard Library header <cctype> (which is the one you want to use) and the std::isalnum in the C++ Standard Library header <locale> (which is not the one you want to use, unless you want to perform locale-specific string processing):

s.erase(std::remove_if(s.begin(), s.end(), (int(*)(int))std::isalnum), s.end());

This works equally well with any of the sequence containers (including std::string, std::vector and std::deque). This idiom is commonly referred to as the "erase/remove" idiom. The std::remove_if algorithm will also work with ordinary arrays. The std::remove_if makes only a single pass over the sequence, so it has linear time complexity.

2 of 12
19

Previous uses of std::isalnum won't compile with std::ptr_fun without passing the unary argument is requires, hence this solution with a lambda function should encapsulate the correct answer:

s.erase(std::remove_if(s.begin(), s.end(), 
 -> bool { return !std::isalnum(c); } ), s.end());
🌐
Cplusplus
cplusplus.com › forum › general › 162526
string - replace all NON alpha numeric w - C++ Forum
I have a near identical function with erase which works fine - the erase one simply erases all nonalnum including spaces. Self explanatory what I want this function to do. For some reason it never enters the isalnum test whereas the erase one does. Really confused... ... You know you can assign to strings like they are arrays, right? No reason to call .replace for single characters.
🌐
Programiz
programiz.com › c-programming › examples › remove-characters-string
C Program to Remove all Characters in a String Except Alphabets
Created with over a decade of experience and thousands of feedback. ... #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) { // 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'; } } printf("Output String: "); puts(line); return 0; }
🌐
Medium
medium.com › @shamzaibrahim7 › three-techniques-to-remove-non-alphanumeric-characters-for-palindrome-problem-9ab7a78d8490
Three Techniques to Remove Non-Alphanumeric Characters for Palindrome Problem | by Syed Hamza | Medium
March 27, 2023 - To use regular expressions to remove non-alphanumeric characters, we can define a regular expression pattern that matches all non-alphanumeric characters, and then use the re.sub() function to replace all matches with an empty string.
🌐
Salesforce
trailhead.salesforce.com › trailblazer-community › feed › 0D54S00000A95cWSAR
Remove Non-alphanumeric Characters from Formula Field
April 13, 2021 - Skip to main content · Register now for TDX! Join the must-attend event to experience what’s next and learn how to build it
🌐
Altair
help.altair.com › 2021 › monarch › help › desktop › Remove_Non_Alphanumeric_Characters.htm
Altair Monarch 2021.0 Help - Remove Non Alphanumeric Characters
Removes all characters that are not numbers (0-9) or letters of the alphabet (A-Z/a-z) from a column value. In this example, Description is the original column, and Description Clean is the new column after Remove Non Alphanumeric Characters is applied:
🌐
Wikitechy
wikitechy.com › technology › remove-non-alphanumeric-characters
[ Solved -7 Answers] - How to remove non-alphanumeric characters from a string - Wikitechy
October 23, 2018 - To remove non-alphanumeric characters from a string,first you need to basically defined it as a regex. [pastacode lang=”javascript” manual=”preg_replace("/[^A-Za-z0-9 ]/", ”, $string); ” message=”JAVASCRIPT CODE” highlight=”” provider=”manual”/]
🌐
Vultr Docs
docs.vultr.com › cpp › examples › remove-all-characters-in-a-string-except-alphabets-
C++ Program to Remove all Characters in a String Except Alphabets. | Vultr Docs
December 11, 2024 - #include <iostream> #include <cctype> // For isalpha function using namespace std; int main() { string srcStr = "Hello 432, World!"; string destStr; for (char c : srcStr) { if (isalpha(c)) { destStr += c; } } cout << destStr << endl; // Outputs: HelloWorld return 0; } Explain Code · This example leverages basic loop iteration, checking each character and assembling a new string exclusively with alphabetic characters. Removing all characters from a string except alphabets in C++ can be achieved utilizing various techniques.