I'm having trouble writing a program that goes through a string and puts each word on a new line and replaces the characters " { [ ( with what there names are like quote brace bracket paren and prints that on a new line the problem i am having is printing out the other words. I can print the first word but not the second or third. So if i have the string
"go 4 expert" it should print out
Code:
quote
go
4
expert
quote
my program prints out
here is my source
Code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
bool isnum(string x)
{
for(int a=0;a<(int)x.size()-1;a++)
{
if(isdigit(x[a]))
{
return true;
}
else if(x[a]=='.')
{
return true;
}
else
return false;
}
return false;
}
int _tmain(int argc, _TCHAR* argv[])
{
string s,t,temp;
temp="";
bool quote=false;
bool paren=false;
bool bracket=false;
bool brace=false;
bool endQuote=false;
bool endParen=false;
bool endBracket=false;
bool endBrace=false;
cout<<"enter string"<<endl;
size_t pos =0;
size_t n;
while(cin>>t)
{
while(pos <t.size())
{
size_t len;
n=t.find_first_of("\"(){}[]",pos);
if(n==pos)
len=1;
else if(n==string::npos)
len=t.size()-pos;
else
len=n-pos;
s=t.substr(pos,len);
pos += s.size();
switch(s.at(0))
{
case '"':
if(!quote)
{
quote=true;
cout<<"quote"<<endl;
continue;
}
else
{
endQuote=true;
}
break;
case '(':
if(!paren)
{
paren=true;
cout<<"paren"<<endl;
continue;
}
else
{
endParen=true;
}
break;
case '[':
if(!bracket)
{
bracket=true;
cout<<"bracket"<<endl;
continue;
}
else
{
endBracket=true;
}
break;
case '{':
if(!brace)
{
brace=true;
cout<<"brace"<<endl;
continue;
}
else
{
endBrace=true;
}
break;
default:
cout<<s<<endl;
break;
}
}
}
return 0;
}