erasing a period or comma from a string then moving it to the end

Write your question here.
I feel like I'm going about this all wrong there are clearly methods that are meant for this i just havent seen enough examples i think in my use case here's what i have now im attempting to take a period or comma from a string after its been converted to pig latin then add it to the end of the string using an input and output file
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
  #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';
}
its easier to just remove it and add it back.
for a c-string memmove is the way to do it.
something like
1
2
3
4
5
6
7
8
9
do
{
char*cp = strstr(input, ".");
if(cp)
{
 memmove(cp, *cp[1], strlen(cp)); //I think. check the boundaries to be sure I did that right. 
 strcat(cp, ".");
}
} while (cp);


your code is a bit wordy in places. Its not wrong, just bloated.
eg
1
2
3
4
5
6
7
8
9
10
11
12
13
bool IsPeriod(char letter)
 {
    return (letter == '.');
 } 

bool IsVowel(char letter)
{ //not sure if this is any better, but bouncing the idea around.. something like
 static char vowels[] = "AEIOU";
 char s[] = {toupper(letter),0};
 return (bool)(strstr(vowels,s));
//its probably just as good (or even better) to do what you did, using the 
//toupper idea to cut the case compares in half?  Anyway, its an idea. 
}


This mixing of char* and strings is odd. Can you just use strings everywhere?
Last edited on
this would go under the punctuation string after the if statement yea?
the period remover is just how-to using char*. If you can, I would use the string version (do you need to see it?) You can put it where you want to remove them. Seems like you would do it right after you get the string?

this gets rid of them all in a string. But you need to count how many you remove to add them back...
s.erase(std::remove(s.begin(), s.end(), '.'), s.end());
for(all the periods)
s += '.';

You had something like this already, and given what you have I suspect you can count the periods yourself for the above... what is it you are not sure how to do, does this cover it??
Last edited on
it would be greatly appreciated to see it if you could

my idea was that each word is a string with the spaces marking the end of string so really only a maximum of one comma or period could really be possible assuming correct grammar and English syntax. i figured i could just get away with blanket removing the period or comma in a given string and then simply adding it to the end but like you said its probably better to just use the string version, but i see what you mean im getting a compiler error : cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'char' for argument '1' to 'bool IsPeriod(char)'|


Last edited on

to remove all the periods, you just need (string style).
//count it, remove them, put them back.
int count = count_if (s.begin(), s.end(), isperiod); //thankfully you already had the isperiod
s.erase(std::remove(s.begin(), s.end(), '.'), s.end());
for(i = 0; i< count; i++) s+= '.';

the top few string functions I use in most code; there are many more but these I use all the time.

substr //not sure, I used memcpy here
find //as strstr
+ and += (as strcat)
length //as strlen
[index] //get a single letter

Last edited on
Thank you so much for all of the help I really appreciate it
Topic archived. No new replies allowed.