Const_Int
Const_Int

Reputation: 23

first print all numbers in given sequence, then all characters and other symbols using Array

How to print first all the numbers in given sequence, then all the characters and letters using only an array and not a string in C++ ???

For Example

intput: 5 (size of array) --> a 1 2 r 4

output: 1 2 4 a r

input: 10 --> a s d @ # $ 7 8 9 1 0

output: 7 8 9 1 0 a s d @ # $
#include <iostream>
 
int main()
{
    int n, c, j = 0, m = 0, a = 0;
    char arr[1000];
        std::cin >> n; 
        for (int i = 0; i < n; ++i)
            {
                std::cin >> arr[i];
                std::cout << arr[i] << " ";
            }
}

Upvotes: 0

Views: 387

Answers (5)

Sharath Chander P
Sharath Chander P

Reputation: 169

#include <bits/stdc++.h>
using namespace std;

#include <iostream>
 
int main()
{
    int n;
    int arr[1000];
    std::cin >> n; 
    int t[1000];
    int count=0;
    for (int i = 0; i < n; ++i)
        {
            char temp;
            cin>>temp;
            if((int(temp)>=91 && int(temp)<=96) || (int(temp)>=58 && int(temp)<=64)||(int(temp)>=33 && int(temp)<=47)){ 
                t[count]=int(temp);
                count++;
            }
            else
            {
                arr[i]=int(temp);
            }
        }
        
    sort(arr,arr+n);
    
    for (int i = 0; i < n; ++i){
        cout<<char(arr[i])<<" ";
    }
    for (int i = 0; i < n; ++i){
        cout<<char(t[i])<<" ";
    }
}

Get the input as "char" and typecast it to "int".if int value represents a special character then store it in another array .Then perform a sort based on integer values and finally while your printing typecast to "char" again.

Upvotes: 0

Const_Int
Const_Int

Reputation: 23

     ```
       #include <iostream>
       int main() {
       int n, c, j = 0, m = 0, a = 0;
       char A[1000], B[1000], C[1000];
       std::cin >> n;

       for (int i=0; i<n; ++i)
        {
            std::cin >> A[i];
        }

       for (int i = 0; i < n; ++i)
        {
            if(A[i] >= '0' && A[i] <= '9')
             {
               B[j] = A[i];
               j++; 
               c = j;
              }    
            else 
            {
               C[m] = A[i];
               m++;
            }
        }

        for (int i = 0; i < n; ++i) 
    {
      A[i] = B[i];
      }
        for (int i = c; i < n; ++i) 
    {
      A[i] = C[a];
      a++;
    }
        for (int i = 0; i < n; ++i)
     std::cout << A[i] << " ";   
    }

Upvotes: 0

clyne
clyne

Reputation: 752

If you'd like to stick with for loops for now, just clean up your code by using some helpers from the cctype header:

#include <cctype>

for (int i = 0; i < n; i++) {
    if (std::isdigit(arr[i]))
        std::cout << (char)arr[i] << ' ';
}
for (int i = 0; i < n; i++) {
    if (std::isalpha(arr[i]))
        std::cout << (char)arr[i] << ' ';
}
for (int i = 0; i < n; i++) {
    if (!std::isdigit(arr[i]) && !std::isalpha(arr[i]))
        std::cout << (char)arr[i] << ' ';
}

This code definitely isn't optimal though, as you can see by the others' more elegant solutions with std::partition. Once you gain more confidence in your skills, consider trying out an algorithmic approach: the standard library has many options. std::partition does work well here, but you could also try a sort or remove algorithm instead. All these functions need are the range of your data (in your case, &arr[0] or arr is the start of the range, and arr + n is the end), and usually some function to specify how you want your data partitioned, sorted, etc.

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 311048

It seems you mean the following. That is the assignment can be easy done using the standard algorithm std::partition applied two timed to the entered sequence. Ot if you want to keep the order of symbols in the same category that you can use the algorithm std::stable_partition. Just substitute std::partition for std::stable_partition in the program below.

#include <iostream>
#include <algorithm>
#include <iterator>
#include <cctype>

int main() 
{
    const size_t N = 1000;
    char a[N];
    
    size_t n = 0;
    
    std::cin >> n;
    
    if ( N < n ) n = N;
    
    for ( size_t i = 0; i < n; i++ )
    {
        std::cin >> a[i];
    }
    
    auto it = std::partition( a, a + n, []( unsigned char c ){ return ::isdigit( c ); } );
    std::partition( it, a + n, []( unsigned char c ){ return ::isalpha( c ); } );
    
    for ( size_t i = 0; i < n; i++ )
    {
        std::cout << a[i] << ' ';
    }
    std::cout << '\n';
    
    return 0;
}

If to enter the sequence

10
a s d @ # $ 7 8 9 1 0

then the program output will be

1 9 8 7 a s d @ $ # 

Here is the same program but with using std::stable_partition.

#include <iostream>
#include <algorithm>
#include <iterator>
#include <cctype>

int main() 
{
    const size_t N = 1000;
    char a[N];
    
    size_t n = 0;
    
    std::cin >> n;
    
    if ( N < n ) n = N;
    
    for ( size_t i = 0; i < n; i++ )
    {
        std::cin >> a[i];
    }
    
    auto it = std::stable_partition( a, a + n, []( unsigned char c ){ return ::isdigit( c ); } );
    std::stable_partition( it, a + n, []( unsigned char c ){ return ::isalpha( c ); } );
    
    for ( size_t i = 0; i < n; i++ )
    {
        std::cout << a[i] << ' ';
    }
    std::cout << '\n';
    
    return 0;
}

The program output for the same input sequence will look like

7 8 9 1 a s d @ # $ 

Upvotes: 1

cigien
cigien

Reputation: 60238

You could first partition the array to place all the digits at the beginning:

auto nums = std::stable_partition(arr, arr + n, 
                                  [](unsigned char c) { return std::isdigit(c); });

and then partition the remaining part of the array to get the characters:

std::stable_partition(nums, arr + n, 
                      [](unsigned char c) { return std::isalpha(c); });

and then just print everything out:

std::copy(arr, arr + n, std::ostream_iterator<char>(std::cout, " "));

Upvotes: 1

Related Questions