How to remove the letters (all non-numeric) from a string in C++

2 Answers

0 votes
#include <iostream>

using namespace std;

char *RemoveCharacters(char* s);

int main()
{
	char s[] = "123c++ 9982programming 10000000000001language";
	
	cout << RemoveCharacters(s) << endl;

	cout << s << endl;

	return 0;
}
char *RemoveCharacters(char* s)
{
	char *dest = s;
	char *src = s;

	while (*s)
	{
		if (!isdigit(*s)) { s++; continue; }
		*dest++ = *s++;
	}
	*dest = '\0';

	return src;
}


/*
run:

123998210000000000001
123998210000000000001

*/

 



answered Jan 31, 2017 by avibootz
0 votes
#include <iostream>

using namespace std;

void RemoveCharacters(char* s);

int main()
{
	char s[] = "123c++ 9982programming 10000000000001language";
	
	RemoveCharacters(s);

	cout << s << endl;

	return 0;
}
void RemoveCharacters(char* s)
{
	char *dest = s;

	while (*s)
	{
		if (!isdigit(*s)) { s++; continue; }
		*dest++ = *s++;
	}
	*dest = '\0';
}


/*
run:

123998210000000000001

*/

 



answered Jan 31, 2017 by avibootz

Related questions

2 answers 256 views
1 answer 165 views
2 answers 171 views
1 answer 207 views
...