#include <iostream>
using namespace std;
int main() {
string x;
cin >> x;
char ch;
/* how can i remove the last comma? */
int l = x.length();
for (int i = 0; i < l; i++) {
ch = x.at(i);
cout << ch << ",";}
return 0;
}
I expect:
input: 1234
output: 1,2,3,4
but now:
input: 1234
output: 1,2,3,4,
>Solution :
Do this(Basically you print the comma separately between that you check if it is the last iteration and works for any numbers):
#include <iostream>
using namespace std;
int main() {
string x;
cin >> x;
char ch;
/* how can i remove the last comma? */
int l = x.length();
for (int i = 0; i < l; i++) {
ch = x.at(i);
cout << ch ;
if (i==l-1) {break;}
cout << ",";}
return 0;
}