make program to delete all number in text

Pages: 12
Jun 1, 2017 at 8:27am
> why I can't simply write char *p = remove_if( s, s + strlen( s ), isdigit );

A standard library header may include other standard library headers.

If both <cctype> and <locale> are included, std::isdigit (in isolation) is ambiguous.
http://en.cppreference.com/w/cpp/string/byte/isdigit
http://en.cppreference.com/w/cpp/locale/isdigit

Note: <locale> is typically included by <iostream>


This will work:
#include <ctype.h> // C header; deprecated
and then *remove_if( s, s + strlen( s ), ::isdigit ) = 0 ;
Last edited on Jun 1, 2017 at 8:29am
Jun 1, 2017 at 8:39am
Thank-you for the explanation, @JLBorges - I had wondered if it was something to do with isdigit strictly returning an int rather than a bool, but I guess that the algorithms are happy enough with the implicit conversion.

The null termination in the same line is quite neat - I missed that.

Revised version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <algorithm>
#include <cctype>
#include <cstring>
using namespace std;

//======================================================================

void noNumbers( char *s )
{
   *remove_if( s, s + strlen( s ), ::isdigit ) = 0;
}

//======================================================================

int main()
{
   const int SIZE = 101;
   char text[SIZE];
   cout << "Enter some text: ";
   cin.getline( text, SIZE );
   noNumbers( text );
   cout << "De-numbered text is " << text;
}


Enter some text: abc123def456ghi
De-numbered text is abcdefghi
Last edited on Jun 1, 2017 at 9:04am
Jun 1, 2017 at 11:41am
@keskiverto

1
2
3
4
5
6
char * pdigit = find_digit(cstr); 
while( pdigit )
 { cstr = pdigit; 
move_left(cstr); 
pdigit = find_digit(cstr); 
}

and can you explain to me what the point of using pointer pdigit here?

and also is there algorithm behind this program..? like how to think to produce code like that... or just using logic and implement it in c sntax? ..?

and thankyou for explaining in really easy to understand way!!!!!!!!!!!!!!!!!!

in conclusion if the while dont have assignment "while(find_digit(cstr)", the return copy from find_digit is just to determine the condition of the while loop as long as not NULL , but the pointer that pass to the move_left function is the one that delete_digit has and it doesnt have any relation with the return copy pointer from find_digit
but the move_left will result in change of the array so i see changes in delete_digit

but if i use "while(cstr=find_digit(cstr)" the return pointer from find_digit will be insert to cstr(local variable) again so it will change the pointer in delete digit!!!!
and the move_left will result in change of the array so i see changes in delete_digit

THANKYOU to everyone that help me to understand,, know its clearer,, and also this pretty sum up what i have been learned so far right now!!! またよろしくお願いいたします。!!!
Last edited on Jun 1, 2017 at 11:51am
Jun 1, 2017 at 1:57pm
what the point of using pointer pdigit
1
2
3
4
5
6
auto pdigit = find_digit( cstr );
cstr = pdigit;

// versus

cstr = find_digit( cstr );

The point is to help to see what we are dealing with.


Note that the program of JLBorges had four functions that each did use name cstr for something. There is nothing wrong in that, if you notice that parameters were passed by value and thus understand that each instance of cstr is independent. However, while still in learning phase, the use of different names would emphasize the distinction.


Algorithm?
I would rather call it syntactic sugar. Making the code look different for illustrative purposes without effectively changing it.
Jun 1, 2017 at 3:02pm
then after that ++src in for loop... , is ++src and src++ result same in this for loop?
i know that if i change to ++dst will result error

Since my code doesn't use the result of the ++src expression, the effect is the same as src++.

When I used dst++, I was using the value of that expression (the value of dst before the increment). Changing it to ++dst changes the code because the value of that expression is the value of dst after the increment.
Jun 4, 2017 at 10:09am
I got it!! thankyou so much everyone for helping me!!!!!!!!!!!!!!
i start to grasp pointer too!!!
thankyou so much!!!!! :D
Topic archived. No new replies allowed.
Pages: 12