Skip to main content
added 2 characters in body
Source Link
Vlad from Moscow
  • 313.1k
  • 27
  • 204
  • 358

To remove characters '#' and '$' you can use standard algorithm std::remove_if. However take into account that if there is for example the following string "12#34" then after removing '#' you will ge "1234". If you need that the resulted string will look as "12 34" or "12:34" then instead of std::remove_if it is better to use std::replace_if.

Below there is a sample code that performs the task. You need to include headers

#include <iostream> #include <cstdlib> #include <cstring> #include <algorithm> int main() { char s[] = "102:$$330:#3133:76531:451:000:$12:44412"; std::cout << s << std::endl; char *p = std::remove_if( s, s + std::strlen( s ), []( char c ) { return ( c == '$' || c == '#' ); } ); *p = '\0'; std::cout << s << std::endl; const size_t N = 8; int a[N]; p = s; for ( size_t i = 0; i < N; i++ ) { a[i] = strtol( p, &p, 10 ); if ( *p == ':' ) ++p; } for ( int x : a ) std::cout << x << ' '; std::cout << std::endl; } 

The output is

102:$$330:#3133:76531:451:000:$12:44412 102:330:3133:76531:451:000:12:44412 102 330 3133 76531 451 0 12 44412 

To remove characters '#' and '$' you can use standard algorithm std::remove_if. However take into account that if there is for example the following string "12#34" then after removing '#' you will ge "1234". If you need that the resulted string will look as "12 34" or "12:34" then instead of std::remove_if it is better to use std::replace_if.

Below there is sample code that performs the task. You need to include headers

#include <iostream> #include <cstdlib> #include <cstring> #include <algorithm> int main() { char s[] = "102:$$330:#3133:76531:451:000:$12:44412"; std::cout << s << std::endl; char *p = std::remove_if( s, s + std::strlen( s ), []( char c ) { return ( c == '$' || c == '#' ); } ); *p = '\0'; std::cout << s << std::endl; const size_t N = 8; int a[N]; p = s; for ( size_t i = 0; i < N; i++ ) { a[i] = strtol( p, &p, 10 ); if ( *p == ':' ) ++p; } for ( int x : a ) std::cout << x << ' '; std::cout << std::endl; } 

The output is

102:$$330:#3133:76531:451:000:$12:44412 102:330:3133:76531:451:000:12:44412 102 330 3133 76531 451 0 12 44412 

To remove characters '#' and '$' you can use standard algorithm std::remove_if. However take into account that if there is for example the following string "12#34" then after removing '#' you will ge "1234". If you need that the resulted string will look as "12 34" or "12:34" then instead of std::remove_if it is better to use std::replace_if.

Below there is a sample code that performs the task. You need to include headers

#include <iostream> #include <cstdlib> #include <cstring> #include <algorithm> int main() { char s[] = "102:$$330:#3133:76531:451:000:$12:44412"; std::cout << s << std::endl; char *p = std::remove_if( s, s + std::strlen( s ), []( char c ) { return ( c == '$' || c == '#' ); } ); *p = '\0'; std::cout << s << std::endl; const size_t N = 8; int a[N]; p = s; for ( size_t i = 0; i < N; i++ ) { a[i] = strtol( p, &p, 10 ); if ( *p == ':' ) ++p; } for ( int x : a ) std::cout << x << ' '; std::cout << std::endl; } 

The output is

102:$$330:#3133:76531:451:000:$12:44412 102:330:3133:76531:451:000:12:44412 102 330 3133 76531 451 0 12 44412 
added 29 characters in body
Source Link
Vlad from Moscow
  • 313.1k
  • 27
  • 204
  • 358

To remove characters '#' and '$' you can use standard algorithm std::remove_if. However take into account that if there is for example the following string "12#34" then after removing '#' you will ge "1234". If you need that the resulted string will look as "12 34" or "12:34" then instead of std::remove_if it is better to use std::replace_if.

Below there is sample code that performs the task. You need to include headers

#include <iostream> #include <cstdlib> #include <cstring> #include <algorithm> int main() { char s[] = "102:$$330:#3133:76531:451:000:$12:44412";  std::cout << s << std::endl; auto p  char *p = std::remove_if( s, s + std::strlen( s ),   []( char c ) { return ( c == '$' || c == '#' ); } );  *p = '\0';  std::cout << s << std::endl;  const size_t N = 8;  int a[N];  p = s;  for ( size_t i = 0; i < N; i++ )  {   a[i] = strtol( p, &p, 10 );   if ( *p == ':' ) ++p;  }  for ( int x : a ) std::cout << x << ' ';  std::cout << std::endl; } 

The output is

102:$$330:#3133:76531:451:000:$12:44412 102:330:3133:76531:451:000:12:44412 102 330 3133 76531 451 0 12 44412 

To remove characters '#' and '$' you can use standard algorithm std::remove_if. However take into account that if there is for example the following string "12#34" then after removing '#' you will ge "1234". If you need that the resulted string will look as "12 34" or "12:34" then instead of std::remove_if it is better to use std::replace_if.

Below there is sample code that performs the task. You need to include headers

#include <iostream> #include <cstdlib> #include <cstring> #include <algorithm> char s[] = "102:$$330:#3133:76531:451:000:$12:44412"; std::cout << s << std::endl; auto p = std::remove_if( s, s + std::strlen( s ), []( char c ) { return ( c == '$' || c == '#' ); } ); *p = '\0'; std::cout << s << std::endl; const size_t N = 8; int a[N]; p = s; for ( size_t i = 0; i < N; i++ ) { a[i] = strtol( p, &p, 10 ); if ( *p == ':' ) ++p; } for ( int x : a ) std::cout << x << ' '; std::cout << std::endl; 

The output is

102:$$330:#3133:76531:451:000:$12:44412 102:330:3133:76531:451:000:12:44412 102 330 3133 76531 451 0 12 44412 

To remove characters '#' and '$' you can use standard algorithm std::remove_if. However take into account that if there is for example the following string "12#34" then after removing '#' you will ge "1234". If you need that the resulted string will look as "12 34" or "12:34" then instead of std::remove_if it is better to use std::replace_if.

Below there is sample code that performs the task. You need to include headers

#include <iostream> #include <cstdlib> #include <cstring> #include <algorithm> int main() { char s[] = "102:$$330:#3133:76531:451:000:$12:44412";  std::cout << s << std::endl;   char *p = std::remove_if( s, s + std::strlen( s ),   []( char c ) { return ( c == '$' || c == '#' ); } );  *p = '\0';  std::cout << s << std::endl;  const size_t N = 8;  int a[N];  p = s;  for ( size_t i = 0; i < N; i++ )  {   a[i] = strtol( p, &p, 10 );   if ( *p == ':' ) ++p;  }  for ( int x : a ) std::cout << x << ' ';  std::cout << std::endl; } 

The output is

102:$$330:#3133:76531:451:000:$12:44412 102:330:3133:76531:451:000:12:44412 102 330 3133 76531 451 0 12 44412 
Source Link
Vlad from Moscow
  • 313.1k
  • 27
  • 204
  • 358

To remove characters '#' and '$' you can use standard algorithm std::remove_if. However take into account that if there is for example the following string "12#34" then after removing '#' you will ge "1234". If you need that the resulted string will look as "12 34" or "12:34" then instead of std::remove_if it is better to use std::replace_if.

Below there is sample code that performs the task. You need to include headers

#include <iostream> #include <cstdlib> #include <cstring> #include <algorithm> char s[] = "102:$$330:#3133:76531:451:000:$12:44412"; std::cout << s << std::endl; auto p = std::remove_if( s, s + std::strlen( s ), []( char c ) { return ( c == '$' || c == '#' ); } ); *p = '\0'; std::cout << s << std::endl; const size_t N = 8; int a[N]; p = s; for ( size_t i = 0; i < N; i++ ) { a[i] = strtol( p, &p, 10 ); if ( *p == ':' ) ++p; } for ( int x : a ) std::cout << x << ' '; std::cout << std::endl; 

The output is

102:$$330:#3133:76531:451:000:$12:44412 102:330:3133:76531:451:000:12:44412 102 330 3133 76531 451 0 12 44412