#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main (){
int x=1;
string str;
cout<<"Enter a statement:";
getline(cin,str);//Use this to input for strings
char* ntr=new char[str.size()+1];//Declaring a char array
// with str's size + 1 on the heap
strcpy (ntr, str.c_str());//Copy str's contents to ntr
char* pch;
pch=strtok(ntr," ");//first break
while(pch!=NULL){//strtok returns NULL if it can't break ntr's char
//array, so if pch is not null, dat means the 1st break was successful
cout<<pch<<endl;//show the result of the 1st break
pch=strtok(NULL," ");//next break,
}
delete []ntr;//cleanup
return 0;
}
Aha! It works perfectly except I would like to place the words into an array word by word, rather than letter by letter. Thanks for your help so far!
This is what I have and have changed so far:
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main (){
int x=1;
string myString;
cout<<"Enter a question or statement for the AI to process:";
getline(cin,myString);
char* myArray=new char[myString.size()+1];//Declaring a char array with str's size + 1
strcpy (myArray, myString.c_str());//Copy str's contents to the array
char* mySentenceBreak;
mySentenceBreak=strtok(myArray," ");//first break
while(mySentenceBreak!=NULL){//strtok returns NULL if it can't break ntr's char array, so if pch is not null, dat means the 1st break was successful
cout<<myArray[1]<<endl;
mySentenceBreak=strtok(NULL," ");//next break,
}
delete []myArray;//cleanup
return 0;
}