Hi,

I need to sort a string of symbols and numbers in to two different strings, first one with numbers, second with symbols. I have written the following code, but it only passes the first type into its string and gives random characters for the second type. Example: if i type "1234abc" it return Numbers:1234 Symbols:random stuff. Same goes for opposite if i type "abc1234" it will return Numbers:random stuff Symbols:abc. Can someone please help me with this. Code is bellow.

#include <cstdlib>
#include <iostream>
#include <cctype>
#define n 99
#define m 99
#define b 99
using namespace std;

int main()
{
    char x[n],y[m],z[b];

    int k,s=0,d=0;
    cout<<"Type in your characters: ";
    cin.getline(x,n);
    k=strlen(x);
    
    
    for (int i=0; i<k; i++){
             if(x[i]>='0' && x[i]<='9'){
              
              y[i]=x[i];
              s++;
              
          }else{                  
              
          z[i]=x[i];
          d++;
          }                  
   } 
    
    cout<<"Numbers: ";
    
    for (int i=0; i<s; i++){    
    cout<<*(y+i);
    }
  
  
  cout<<endl<<"Symbols: ";
  
  for(int i=0; i<d; i++){
         cout<<*(z+i);
          }
          
          
  cout<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

Hi,

I need to sort a string of symbols and numbers in to two different strings, first one with numbers, second with symbols. I have written the following code, but it only passes the first type into its string and gives random characters for the second type. Example: if i type "1234abc" it return Numbers:1234 Symbols:random stuff. Same goes for opposite if i type "abc1234" it will return Numbers:random stuff Symbols:abc. Can someone please help me with this. Code is bellow.

#include <cstdlib>
#include <iostream>
#include <cctype>
#define n 99
#define m 99
#define b 99
using namespace std;

int main()
{
    char x[n],y[m],z[b];

    int k,s=0,d=0;
    cout<<"Type in your characters: ";
    cin.getline(x,n);
    k=strlen(x);
    
    
    for (int i=0; i<k; i++){
             if(x[i]>='0' && x[i]<='9'){
              
              y[i]=x[i];
              s++;
              
          }else{                  
              
          z[i]=x[i];
          d++;
          }                  
   } 
    
    cout<<"Numbers: ";
    
    for (int i=0; i<s; i++){    
    cout<<*(y+i);
    }
  
  
  cout<<endl<<"Symbols: ";
  
  for(int i=0; i<d; i++){
         cout<<*(z+i);
          }
          
          
  cout<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

The problem is in your for loop on line 19. When i is incremented for each loop, the two conditions of the if statement become out of phase with "i", which means if numbers are first then i keeps being incremented leaving z behind and vice versa. I'm not sure if that is a very clear explanation, but here is the correction to your code and see if you can determine why. If not, post back and I'll try to explain it better.

for (int i=0; i<k; i++){
    if(x[i]>='0' && x[i]<='9'){
     
    y[s]=x[i];     //s replaces i;
    s++;
     
    }else{
     
    z[d]=x[i];     //d replaces i;
    d++;
    }

Edited 4 Years Ago by MandrewP: Spelling thing....

Hi,

I need to sort a string of symbols and numbers in to two different strings, first one with numbers, second with symbols. I have written the following code, but it only passes the first type into its string and gives random characters for the second type. Example: if i type "1234abc" it return Numbers:1234 Symbols:random stuff. Same goes for opposite if i type "abc1234" it will return Numbers:random stuff Symbols:abc. Can someone please help me with this. Code is bellow.

#include <cstdlib>
#include <iostream>
#include <cctype>
#define n 99
#define m 99
#define b 99
using namespace std;

int main()
{
    char x[n],y[m],z[b];

    int k,s=0,d=0;
    cout<<"Type in your characters: ";
    cin.getline(x,n);
    k=strlen(x);
    
    
    for (int i=0; i<k; i++){
             if(x[i]>='0' && x[i]<='9'){
              
              y[i]=x[i];
              s++;
              
          }else{                  
              
          z[i]=x[i];
          d++;
          }                  
   } 
    
    cout<<"Numbers: ";
    
    for (int i=0; i<s; i++){    
    cout<<*(y+i);
    }
  
  
  cout<<endl<<"Symbols: ";
  
  for(int i=0; i<d; i++){
         cout<<*(z+i);
          }
          
          
  cout<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

BTW, if you don't mind a little picky constructive criticism, then read this:

It is better not to use the #define directive, because there is no type checking. It would be better to use a constant instead. Also, as Mike 2000 7 stated, when incrementing a variable, like s++ or d++, it is preferred to use the pre increment when it doesn't matter. It makes no difference functionally whether you use s++ or ++s, because it translates into machine code exactly the same way when used by itself on a line, but the pre increment (++s) is just the preferred way (but nobody is going to shoot you over it : )

#define n 99   //constant int n = 99;
#define m 99   //constant int m = 99;
#define b 99   //constant int b = 99;

 y[i]=x[i];
 s++;          //++s;

 z[i]=x[i];
 d++;          //++d;
This article has been dead for over six months. Start a new discussion instead.