rotating punctuation marks

Feb 8, 2019 at 12:03pm
how would I find punctuation string and then if there is punctuation d move it to the end of a string?

Feb 8, 2019 at 12:16pm
You can do that simply with string operations
std::string::find : http://www.cplusplus.com/reference/string/string/find/
std::string::erase : http://www.cplusplus.com/reference/string/string/erase/
std::string::append : http://www.cplusplus.com/reference/string/string/append/

Keep in mind that to append you can also do this:
1
2
3
4
5
6
string str = "something";

//appending 's' to str
str.append("s");
//or the following does the same
str += "s";
Feb 8, 2019 at 12:30pm
Would it work the same for an input file such as an unknown string length and unknown punctuation mark?
Feb 8, 2019 at 12:40pm
>Would it work the same for an input file such as an unknown string length

Well yes, you would just need an extra step before trying to search a string for a char.
_first you read the file and store the stream in a string
_then you can start doing operations on your string


>Would it work the same for an input file such as [...] unknown punctuation mark?

No, when you want to find a string within a string you have to specify what you're looking for. As far as I know there is not way of telling whether a string contains punctuation within it other than by manually doing it.

However if you #include <cctype> you can use the function std::ispunct but it takes a char as argument, so I guess you'd have to manually check every char of your string.

std::ispunct : https://en.cppreference.com/w/cpp/string/byte/ispunct
Last edited on Feb 8, 2019 at 12:45pm
Feb 8, 2019 at 1:01pm
ah thank you, so, what I'm doing is a piglatin translator i figure based on what you said if i first, translate the input into pig latin then create two strings that are before and after the punctuation mark id be left with a string that doesnt have the punctuation but could I save a third string using the find function that saves that character as a string and then essentially add it to the end of the combination of the other two string?
Feb 8, 2019 at 1:09pm
I don't quite understand what you mean, Could you provide an example, not necessarily code, of what you're trying to achieve. With your initial string, then after translation etc?
Feb 8, 2019 at 1:18pm
how would i create a string of just the found character?
Feb 8, 2019 at 1:25pm
If you mean when you look for a punctuation char and find it, then
_if at position n your string has a punctuation char,
_you can create a substring aka 'extract' the char and store it in another string ( see http://www.cplusplus.com/reference/string/string/substr/)
_then append that string to your final string which will hold all punctuation marks you've come across
Feb 8, 2019 at 1:52pm
how way off might this be?
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <fstream>
#include <cstring>
#include <string>
#include <cctype>
using namespace std;

bool IsPeriod(char letter)
 {

    switch (letter)
    {
    case '.':
        return true;
    }
    return false;
 }
bool IsComma(char letter)
{
    switch (letter)
    {
    case ',':
        return true;
    }
    return false;
}
bool IsVowel(char letter)
{
    switch (letter)
    {
    case 'A': case 'E': case 'I': case 'O': case 'U': //case 'Y':
    case 'a': case 'e': case 'i': case 'o': case 'u': //case 'y':
        return true;
    }
    return false;
}

string PigLatin(const char *word)
{
    string s(word);
    if (IsVowel(word[0]))
        s = s + "way";
    else
        s = s.substr(1) + s[0] + "ay";

    return s;
}

string Punctuation(string *PigLatin)
{

  string t(PigLatin);
    if (IsPeriod(PigLatin[i]))
   int i
   char chars[] = ".";

   for (unsigned int i = 0; i < tlen(chars); ++i)
   {
   
      t.erase (std::remove(t.begin(), t.end(), chars[i]), t.end());
   }

   
   t= t + ".";
   else if (IsComma(PigLatin[i]))

   char chars[] = ",";

   for (unsigned int i = 0; i < tlen(chars); ++i)
   {
       t.erase (std::remove(t.begin(), t.end(), chars[i]), t.end());
   }
   t= t + ",";

   else

   t=t;

   return t;
}

int main()
{
    const char *delims = " \"";

    ifstream fin("pigLatinFilein.txt");
    ofstream fout("pigLatinFileout.txt");

    char sentence[10000];
    fin.getline(sentence, sizeof sentence);

    char *word = strtok(sentence, delims);
    while (word)
    {
        fout << Punctuation(PigLatin) << " ";
        word = strtok(NULL, delims);
    }
    fout << '\n';
}
Feb 8, 2019 at 2:37pm
Let's start little by little.

For your first three functions:

IsPeriod function:
1
2
3
4
5
6
7
bool IsPeriod(char letter)
 {
    if (letter == '.')
        return true;
    else
        return false;
 }

Switches are only ever useful when you have many statements, surely not one.

IsComma function:
1
2
3
4
5
6
7
bool IsComma(char letter)
{
    if (letter == ',')
        return true;
    else
        return false;
}


IsVowel function:
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
bool IsVowel(char letter)
{
    letter = toupper(letter);
    
    switch (letter)
    {
        case 'A':
            return true;
            break;
        case 'E':
            return true;
            break;
        case 'I':
            return true;
            break;
        case 'O':
            return true;
            break;
        case 'U':
            return true;
            break;
        default:
            return false;
    };
}


Here a switch is ok, also check how to use switches, you need break statements for each cases

For the rest of your code, I have a few questions / comments:
_Why do you want to use cstrings instead of strings?
_Instead of reading the whole file and cutting it down, which looks like a pain, why don't you just read a line, then deal with that line and only after, read the second line and repeat?
_What exactly is your pigLatin function supposed to do?
_What are you trying to do with the punctuation?
Topic archived. No new replies allowed.