Parameter that locates an abbreviation at the end of a sentence

Oct 4, 2019 at 4:37am
I'm working on an assignment that requires me to write a program that will locate abbreviations and replace them with the spelled-out version of the words.
In line 18, for my if statement I can't figure out how to enter my parameter that locates if an abbreviation is located at the end of a sentence.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;

string LOLSpeak(string S, string Abbr, string Replace)
{
    int Loc = S.find(" "+Abbr+" ",0);
    while (Loc != -1)
    {
        S.replace(Loc,Abbr.size()+2," "+Replace+" ");
        Loc = S.find(" "+Abbr+ " ",0);
    }
    
    if (S.substr(0,Abbr.size()+1) == Abbr+" ") 
        {
            S.replace(0,Abbr.size(),Replace);
        }
    
    if (S.substr(,Abbr.size()) == " "+Abbr) 
        {
            S.replace(,Abbr.size(),Replace);
        }
        
        return S;
}

int main()
{
    char Input[80];
    cout << "Enter a sentence: ";
    cin.getline(Input, 80, '\n');
    string Sentence = Input;

    Sentence = LOLSpeak(Sentence, "u", "you");
    Sentence = LOLSpeak(Sentence, "brb", "be right back");
    Sentence = LOLSpeak(Sentence, "lol", "laughing at loud");
    Sentence = LOLSpeak(Sentence, "btw", "by the way");
    Sentence = LOLSpeak(Sentence, "smh", "shake my head");
    Sentence = LOLSpeak(Sentence, "idk", "I don't know");
    Sentence = LOLSpeak(Sentence, "imo", "in my opinion");
    Sentence = LOLSpeak(Sentence, "fyi", "for your information");
    Sentence = LOLSpeak(Sentence, "wwjd", "what would Jesus do");
    Sentence = LOLSpeak(Sentence, "ttyl", "talk to you later");
    Sentence = LOLSpeak(Sentence, "idc", "I don't care");
    Sentence = LOLSpeak(Sentence, "ily", "I love you");
    Sentence = LOLSpeak(Sentence, "jk", "just kidding");

    cout << Sentence << endl;
}
Last edited on Oct 5, 2019 at 7:34pm
Oct 4, 2019 at 12:03pm
Hello nubester01,

The first thing I would do is change the beginning of "main" to:
1
2
3
4
5
6
std::string input{ "" }; // <--- Add a sentence in the "" to work with.

cout << "Enter a sentence: ";
//std::getline(std::cin, input);

string Sentence = input;

I am not sure if sending a "char" array to a function and defining the variable in the function as a "std::string" will convert it or not. I will have to try it. But if you start with a "std::string" there is no question.

By initializing the string with a value and putting a comment on line 4 you can test your program without having to enter something every time the program runs. And you change the sentence any time you like to try different things. Or comment line 1 and create a different line 1 and switch comments when you need to use something different.

In main calling the function 13 times would work, but it is to much. The function should be designed to check all of those choices and convert the sentence all at one time returning the finished change.

I was noticing in lines 13 and 18 on the rhs of "==" they are different. Is there a reason for this?

In the function the while loop looks like it is checking the whole string and replacing what is needed. The two if statements are out side of the loop and appear to be doing what is already been done and I believe they would be skipped anyhow.

I will have to load up the program and give some testing to see what it is doing.

Hope that helps,

Andy
Oct 4, 2019 at 12:33pm
Hello nubester01,

For what it is worth when I compiled your code I found that lines 18 and 22 are missing the first parameter of the function call.

I also found that you did not include the header file "<string>".

My first runs appeared to work with one abbreviation but not two. and the if statements in the function have not been used yet.

A note: it is generally accepted the regular variable names start with a lower case letter. Classes and structs start with start with a capital letter and constant variables are all caps. It is your choice to name variables as you like, but try to avoid single letter variable names.

Hope that helps,

Andy
Oct 4, 2019 at 12:54pm
nubester01, I would just add an extra space to both ends of the sentence the user input. Then, you can process just based on the single rule. At the end, you trim both ends of the string (remove extra spaces at each end).
Last edited on Oct 4, 2019 at 12:55pm
Oct 4, 2019 at 3:03pm
Hello nubester01,

Ganado has a workable solution, but before I gave that a try I had already found a solution. This should help to make it easier to understand:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>

using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/


string LOLSpeak(string str, string Abbr, string Replace)
{
	size_t Loc = str.find(" " + Abbr, 0);  // <--- Changed.

	while (Loc != std::string::npos)  // <--- Changed.
	{
		str.replace(Loc, Abbr.size() + 2, " " + Replace + " ");
		Loc = str.find(" " + Abbr, 0);  // <--- Changed.
	}

	if (str.substr(Abbr.size() + 1) == Abbr + " ") //1st parameter is starting location, 2nd parameter is number of characters... +1 for space after
	{
		str.replace(0, Abbr.size(), Replace);
	}

	if (str.substr(0, Abbr.size()) == " " + Abbr) //1st parameter is starting location, 2nd parameter is number of characters... +1 for space after
	{
		str.replace(0, Abbr.size(), Replace);
	}

	return str;
}

int main()
{
	std::string sentence{ "btw This is a test lol for u to see what u happens brb ily." };

	//cout << "Enter a sentence: ";
	cout << "Enter a sentence: " << sentence << "\n\n";
	//getline(std::cin, sentence);
	//string Sentence = Input; // <--- Not needed.

	sentence = LOLSpeak(sentence, "u", "you");
	sentence = LOLSpeak(sentence, "brb", "be right back");
	sentence = LOLSpeak(sentence, "lol", "laughing at loud");
	sentence = LOLSpeak(sentence, "btw", "by the way");
	sentence = LOLSpeak(sentence, "smh", "shake my head");
	sentence = LOLSpeak(sentence, "idk", "I don't know");
	sentence = LOLSpeak(sentence, "imo", "in my opinion");
	sentence = LOLSpeak(sentence, "fyi", "for your information");
	sentence = LOLSpeak(sentence, "wwjd", "what would Jesus do");
	sentence = LOLSpeak(sentence, "ttyl", "talk to you later");
	sentence = LOLSpeak(sentence, "idc", "I don't care");
	sentence = LOLSpeak(sentence, "ily", "I love you");
	sentence = LOLSpeak(sentence, "jk", "just kidding");

	cout << sentence << endl;

	return 0;
}

Some of the changes have thrown off the line numbers a bit.

Line 10 the function ".find" returns a "size_t" AKA "unsigned int", so the variable should be of the same type.

Line 12 is better written this way and has a better chance of working with different compiles and header files along with, I believe, being more portable.

For lines 10 and 15 I removed the trailing space and it works just fine. You do need the leading space because of "u" otherwise it would replace every "u" in the string with "you".

When I put an abbreviation at the beginning of the string it did use the first if statement. I now see that the second if statement is to find an abbreviation at the end of the string, but it is no longer needed because of the two ".find" functions.

In your OP lines 18 and 20 are missing the first parameter. This can be done with the ".substr" function as it has a default for the first parameter, but you do not need the "," to use it. Look at line 18 in the above code and compare it to line 23 to see the difference.

For the ".replace" you do need to use the first parameter as there is no default here.

Something I have been thinking about starts in "main" by calling the function only once and sending it only the string that is entered. Then in the function you could use two parallel arrays of strings or two vectors, better choice, a "std::map" or something else. The use the first ".find" and the while to check every element of the array, vector or map to change the string. I have no idea what you know. Personally I would lean to the "map" first and the vector second, but an array will work.

I think I have covered everything. The above code should help, but if you have any questions let me know.

Hope that helps,

Andy
Oct 5, 2019 at 7:52pm
Thank you so much! This was easier to understand and definitely helped. I think I kinda get what they are saying about using namespace std; but will have to read into a bit more to make sure I fully understand. I really appreciate your help!
Oct 5, 2019 at 8:54pm
@Handy Andy,
Your code as it stands doesn't seem to produce the result you would want ("btw" isn't changed at the start):
Enter a sentence: btw This is a test lol for u to see what u happens brb ily.

btw This is a test laughing at loud for you to see what you happens be right back I love you 


It will also struggle with any surrounding punctuation.
Last edited on Oct 6, 2019 at 8:40am
Topic archived. No new replies allowed.