0
C++
I am writing a program to convert special characters in the text to underscore. But the program is convert the whole text to single line. I am supposed to use ASCII values in this task. What could be the problem https://code.sololearn.com/cdLtmPw2RomD/?ref=app
3 Respostas
+ 2
You can use `isalnum` function from <cctype> header. It is a function that returns true when given an alphanumeric value as argument. When non alphanumeric value was given as argument, the function returns false.
#include <cctype> // for isalnum()
for( unsigned int i = 0; i < strlen(text); i++)
{
if(!isalnum(text[i]))
{
text[i] = '_';
}
}
http://www.cplusplus.com/reference/cctype/isalnum/
(Edit)
If you were restricted to use only ASCII values, then follow Szwendacz's advice.
+ 1
missing parentheses
for( unsigned int i = 0; i < strlen(text); i++)
{
if(!((text[i]>=48 && text[i]<=57)||(text[i]>=65 && text[i]<=90) || (text[i]>=97 && text[i]<=122)))
{
text[i] = '_';
}
}
+ 1
Super thanks guys