String manipulation

Working to compare two strings. I want to detect if there is a 1 character deletion between the strings and if not return false. My code is not properly detecting deletion errors and skips past this section.
3 type of 1 character deletions (1 char deleted at beginning, middle or end of word). I should account for all these situations but my code has trouble on the ends.

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

#include <iostream>
#include <string>

using namespace std;

int main()

{
bool comparison; 
int difference=0;
string word1, word2; 


cout << "Please enter two strings starting with string1." << endl; 
cin >> word1;
cin >> word2;

if (word1.length() != word2.length())
{
    for(int i=0; i < word1.length(); i++)
     {
       if (word1[i] != word2[i] && word1.substr[i+1] == word2.substr[i])
               comparison = true; 
       else
               comparison = false; 

     }

}
 
if (comparison)
cout << "true.";
else (!comparison)
cout << "false."; 


return 0; 
}


i have also tried doing something like the following but still come up with errors..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

int difference =0; 

if(word1.length() != word2.length())
{

    for (int i=0; i< word1.length(); i++)
      {
         if(word1[i] != word2[i])
            difference +=1;
      }
}

if (difference == 1)
cout << "true"; 
else
cout << "false"; 




Last edited on
I'm no expert but I'll try to help...
I think bool comparison; should be bool comparison = false; ... so the program will print "false" if the strings are the same ( obviously there was no deletion )
1
2
3
4
if (comparison)
cout << "true.";
else (!comparison)
cout << "false."; 

1
2
3
4
if (comparison)
cout << "true.";
else
cout << "false."; 

and be sure that the original string is word1 and the string that suffered a deletion is word2 ... hope this helps a little
Last edited on
Thank you for your assistance! However, I still keep getting error( 1 character deletion errors get sent to my outfile as "many errors" and not "deletion"). I'll post up my two functions and maybe someone else with a fresh eye can spot my logic error. I can post up the rest of the functions if someone wants to see it.Thank you again :D

bool function that detects error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
bool isDeletion (string correctWord, string userWord)
{
int difference =0;

if(correctWord.length()-1 == userWord.length())
{
    for(int i =0; i<correctWord.length();i++)
    {
        if(correctWord[i] != userWord[i])
        {
            difference +=1;

        }
    }
    if (difference == 1)
            return true;
        else
            return false;

}
else
    return false;
}



main function that calls bool for deletion error

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
void testError(ifstream& fin, string correctWord, string userWord,spelling_error errorType, ofstream& fout)
{
int correct = 0;
int substitution =0;
int transposition =0;
int deletion = 0;
int insertion =0;
int error =0;
char ch;

fout << "*****Starting a new line*****" << endl;
fin  >> correctWord;
fout << "The correct word is " << correctWord << endl << endl;

while(!fin.eof())
{
  ch = ' ';

  while(ch != '\n')
  {
      fin >> userWord;
      fout << "The user word is " << userWord << endl;

if(correctWord.length() == userWord.length())
{
    if(correctWord == userWord)
      {
         errorType = CORRECT;
         printError(errorType,correct,substitution,transposition,deletion,insertion,error, fout);
      }
      else if (isSubstitution(correctWord, userWord))
      {
          errorType = SUBSTITUTION;
          printError(errorType,correct,substitution,transposition,deletion,insertion,error, fout);
      }
      else if (isTransposition(correctWord, userWord))
      {
          errorType = TRANSPOSITION;
          printError(errorType,correct,substitution,transposition,deletion,insertion,error, fout);
      }
}
else if(correctWord.length() != userWord.length())
{
      if (isDeletion(correctWord, userWord))
      {
          errorType = DELETION;
          printError(errorType,correct,substitution,transposition,deletion,insertion,error, fout);
      }
      else if(isInsertion(correctWord, userWord))
      {
          errorType = INSERTION;
          printError(errorType,correct,substitution,transposition,deletion,insertion,error, fout);
      }

      else
      {
          errorType = ERROR;
          printError(errorType,correct,substitution,transposition,deletion,insertion,error, fout);
      }
}
fin.get(ch);

  }

fout << "*****Starting a new line*****" << endl;
fin  >> correctWord;
fout << "The correct word is " << correctWord << endl << endl;

}

fout << "There were " << correct << " correct words." << endl;
fout << "There were " << substitution << " substitution errors." << endl;
fout << "There were " << transposition << " transposition errors." << endl;
fout << "There were " << deletion << " deletion errors." << endl;
fout << "There were " << insertion << " insertion errors." << endl;
fout << "There were " << error << " words with many mispellings." << endl;

}





Last edited on
in the bool function...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool isDeletion (string correctWord, string userWord)
{
int difference =0;

if(correctWord.length()-1 == userWord.length())
{
    for(int i =0; i<correctWord.length();i++)
    {
        if(correctWord[i] != userWord[i])
        {
            difference +=1;

        }
    }
    if (difference == 1)
            return true;
        else
            return false;

}
else
    return false;
}


since userWord is 1 char shorter than correctWord and you compare each char ... isn't it possible to compare the last char of correctWord with nothing ? thus creating an error ?
sounds like that's my problem. I'm wondering how i can compare that last character if that is the deletion. I also tried comparing substrings after( i) is not equal but i still run into the problem with the last character if its a deletion. hmm

if (correctWord.length() -1 == userWord.length())

for (i =0; i<userWord.length(); i++)
return true;

I think that solved the problem. Another question lol so i have all my 1 error checks, but a few multiple error words throw it off. for instance two transposition errors in a word or an insertion or deletion. Any suggestions? an error counter for each function?
Last edited on
If there's only one letter left to compare and difference is 0, it's a deletion.

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
bool isDeletion (string correctWord, string userWord)
{
	if ( correctWord.length() == 0 )
		return false ;

	if (correctWord.length() == 1 && userWord.length() == 0 )
		return true ;

	if ( correctWord.length()-1 == userWord.length() )
	{
		unsigned cIdx = 0;
		unsigned uIdx = 0;

		while ( cIdx < correctWord.length() && uIdx < userWord.length() )
		{
			if ( correctWord[cIdx++] != userWord[uIdx++] )
			{
				if ( ++cIdx - uIdx > 1 ) // if this is the 2nd time we're doing this, return false
					return false ;
			}
		}

		return true ;
	}

	return false ;
}


I didn't think it was well suited for a for loop.
Topic archived. No new replies allowed.