【发布时间】:2021-01-28 12:51:50
【问题描述】:
我尝试使用 c++ 分隔给定字符串中的 A-Z 字符,但分隔的字符串未在输出中打印,但如果我在 for 循环内移动“cout”语句,它会打印字符。我不知道为什么会这样。如果我做错了,请告诉我。
我的代码
#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
}
}
【问题讨论】:
-
编译一个调试版本并在你最喜欢的调试器中运行程序。调试器会告诉你出了什么问题
标签: c++ string c++11 output stdstring