mercredi 14 octobre 2020

string is not printing in c++ outside the a for loop

I've tried to separate A-Z character in a given string using c++ but the separated string is not printing in the output but if I shift the "cout" statement inside the for loop it printing the characters. I don't know why its happen. please let me know if I've done any mistake.

my code

#include<iostream>

using namespace std;

int main()
{
    int t;
    cin>>t;    //number of test cases

    while(t--)
    {
        string s,a,n;
        int j=0,k=0;
        char temp;

        cin>>s;      //getting string

        for(int i=0; i<s.length(); i++)
        {

            if(s[i]>=65 && s[i]<=90)       //checking for alphabets
            {
                a[j]=s[i];
                j++;
                cout<<a[j-1]<<endl;
            }

            else
            {
                n[k]=s[i];
                k++;
                cout<<n[k-1]<<endl;
            }

        }

        cout<<endl<<a<<endl<<n;       //this line is not printing
     }
}

Aucun commentaire:

Enregistrer un commentaire