#include <iostream>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
struct stackNode
{
stackNode(std::string s)
{
this->val=s;
next=NULL;
}
std::string val;
stackNode * next;
};
struct stringStack
{
stringStack()
{
top=NULL;
}
bool isEmpty()
{
return (top==NULL);
}
stackNode * top;
void push(std::string s)
{
stackNode * nd=new stackNode(s);
if(top==NULL)
{
top=nd;
}
else
{
nd->next=top;
top=nd;
}
}
void pop()
{
if(top==NULL)
{
return;
}
else
{
stackNode *p=top;
top=top->next;
delete p;
}
}
void printStack()
{
while(!isEmpty())
{
std::cout<<top->val<<std::endl;
pop();
}
}
};
int main()
{
stringStack sstk;
std::string temp="";
bool newWord=false;
char * text="You are son of a bitch.";
char *p=text;
char c=*p++;
while(c!='\0')
{
if(c==' ')
{
newWord=true;
}
else
{
newWord=false;
}
if(newWord==true)
{
if(temp.size()!=0)
{
sstk.push(temp);
temp.clear();
}
c=*p++;
continue;
}
else
{
temp.push_back(c);
c=*p++;
}
}
sstk.push(temp);
sstk.printStack();
temp.clear();
}
逆序一段文本算法
最新推荐文章于 2022-12-21 21:05:09 发布