Reputation: 73
As an assignment, I have to write a function in C++ that deletes a word in a sentence. We are given the sentence itself as a string std::string sentence
and the number of a word to be deleted int place
.
There can be multiple spaces between words and sentence can end and begin with spaces.
For example: DeleteWord("Fox jumped over lazy dog", 2)
should return: "Fox over lazy dog"
.
The problem is that I can only use std::string size() and resize().
I also should delete only the word, not the spaces around it.
I've written some code, but the output is not right: I am putting all words in an array and then deleting the one in an argument but the whitespaces are a big problem as they are being put in the array as well, thus I am not being able to delete and print them correctly.
Do you have any ideas how to solve this problem? Thanks!
My code so far:
std::string DeleteWord(std::string sentence, int place){
std::string retvalue;
long long wordcounter = 0;
long long spacecounter = 0;
long long spacescounter = 0;
int i = 0;
int index = 0;
for(int i=0; i<sentence.size(); i++){
if((sentence[i]==' ') && (sentence[i+1]!=' ')) spacecounter++;
}
for(int i=0; i<sentence.size(); i++){
if(sentence[i]==' ') spacescounter++;
}
std::string words[spacescounter+1];
while(i<sentence.size()){
if(sentence[i]!=' '){
words[index] += sentence[i];
i++;
}
else{
index++;
i++;
}
}
sentence[place-1] = ' ';
for(int i=0; i<=index; i++){
retvalue+=words[i] + ' ';
}
return retvalue;
}
Upvotes: 0
Views: 722
Reputation: 168
I checked the words in the char array by checking the spaces and I delete the words by changing the '\0'(end string) position o by shifting the array
#include <string>
#include <iostream>
#include <string.h>
//#include <stdio.h>
using namespace std;
string DeleteWord(string a,int index)
{
int length=a.size();
int pos;
int word=1;
char* s;
s=new char[length];
strcpy(s,a.c_str());
for(int i=0; i<length; i++)
{
if(s[i]==' ' and i!=0 and s[i-1]!=' ')
word++;
}
if(index>word)
{
delete [] s;
return "error";
}
else
{
if(index==word)
{
pos=0;
for(int i=length-1; i>=0; i--)
{
if(s[i]==' ')
{
pos=i+1;
break;
}
}
s[pos]='\0';
}
else
{
int wl=0;
char pre=' ';
word=0;
pos=-1;
bool found=false;
for(int i=0; i<length; i++)
{
if(pre==' ' and s[i]!=' ')
{
word++;
if(word==index){
found=true;
pos=i;
}
}
if(found==true)
{
if(s[i]==' '){
break;
}
else
wl++;
}
pre=s[i];
}
for(int i=pos; i<length; i++)
s[i]=s[i+wl];
}
string f=string (s);
delete[] s;
return string(f);
}
}
int main()
{
string str="Fox jumped over lazy dog";
cout<<DeleteWord(str,4)<<endl;
return 0;
}
Upvotes: 0
Reputation: 16453
You don't have to count the spaces or words. You don't have to store the words in an array.
Iterate over the sentence and decrement place
if the previous element is a space and the current is a non-space. If place
is 0 you have to skip the word. Else copy the string into returned string. As I said in the comments it's as simple as one loop, one variable, 3 if conditions:
#include <iostream>
#include <string>
std::string DeleteWord(const std::string &sentence, int place){
if (sentence.empty()) {
return std::string();
}
auto isPreviousSpace = std::isspace(sentence[0]);
if (!isPreviousSpace) {
--place;
}
std::string retvalue;
for (const auto &c : sentence) {
if (isPreviousSpace && !std::isspace(c)) {
--place;
}
if (place != 0 || std::isspace(c)) {
retvalue += c;
}
isPreviousSpace = std::isspace(c);
}
return retvalue;
}
int main()
{
std::cout << DeleteWord("Fox jumped over lazy dog", 2);
// Output is: "Fox over lazy dog"
return 0;
}
A C++98 solution without std::isspace
and without const ref argument:
#include <iostream>
#include <string>
std::string DeleteWord(std::string sentence, int place){
if (sentence.empty()) {
return std::string();
}
bool isPreviousSpace = sentence[0] == ' ';
if (!isPreviousSpace) {
--place;
}
std::string retvalue;
for (unsigned int index = 0; index < sentence.size(); ++index) {
if (isPreviousSpace && sentence[index] != ' ') {
--place;
}
if (place != 0 || sentence[index] == ' ') {
retvalue += sentence[index];
}
isPreviousSpace = sentence[index] == ' ';
}
return retvalue;
}
int main()
{
std::cout << DeleteWord("Fox jumped over lazy dog", 2);
// Output is: "Fox over lazy dog"
return 0;
}
Upvotes: 1
Reputation: 2343
Find starting index and ending index of the word need to be deleted, i call it s
and e
respectively, the result will be: sentence[0:s-1] + sentence[e+1->sentence.size()-1]
. I assume input value of place will be valid (greater than zero and equals to or less than number of words in the sentence). You can try with my code:
#include <iostream>
#include <string>
#include <map>
using namespace std;
pair<int,int> startAndEndIndexOfWord(string sentence, int place){
pair<int,int> startAndEnd;
int currentWordIndex = 0;
int startIndex = 0;
int endIndex = 0;
for(int i=0;i<sentence.size();i++){
//meet a word
if(sentence[i]!=' '){
currentWordIndex += 1;
if(currentWordIndex==place){//if it is the word need to be delete
startIndex = i;
//find ending index
endIndex = sentence.size()-1; //just a default value
for(int j=i+1;j<sentence.size();j++){
if(sentence[j]==' '){
endIndex = j-1;
break;
}
}
startAndEnd.first = startIndex;
startAndEnd.second = endIndex;
return startAndEnd;
}
else{
i+=1;
while(sentence[i]!=' ') i++; //ignore the current word
}
}
}
}
string deleteWord(string sentence, int place){
string result;
pair<int,int> startAndEndIndex = startAndEndIndexOfWord(sentence,place);
for(int i=0;i<startAndEndIndex.first;i++){
result+=sentence[i];
}
for(int i=startAndEndIndex.second+1;i<sentence.size();i++){
result+=sentence[i];
}
return result;
}
int main() {
int place = 5;
string sentence = "Fox jumped over lazy dog";
string result = deleteWord(sentence,place);
cout<<result;
return 0;
}
Upvotes: 0