-
October 27th, 2016, 02:53 PM
#1
For loops and if statement to accept word to listbox, needs 2 reoccurring letters
Code:
int i = 0;
// int j = i + 1;
int charCount = 0;
for(i=0; i<word.Length;i++)
{
for ( int j= 1; i < word.Length; j++)
{
if(word.Substring(i)==word.Substring(j))
{
charCount++;
}
}
}
if (charCount!=0)
MessageBox.Show("Word Accepted");
else
MessageBox.Show("Word Accepted");
Last edited by 2kaud; October 27th, 2016 at 03:52 PM.
-
October 27th, 2016, 03:56 PM
#2
Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters
When posting code, please use code tags. Go Advanced, select the formatted code and click '#'.
and your c# question is? Do you really want it to display 'Word Accepted' under any circumstance? - both parts of the if statement display the same message!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 27th, 2016, 04:39 PM
#3
Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters
Sorry, just noticed that if its not equal to 0, it should not accept word into listbox
-
October 27th, 2016, 05:19 PM
#4
Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters
Originally Posted by neymark
Sorry, just noticed that if its not equal to 0, it should not accept word into listbox
Any idea of how i can get it to work?
-
October 28th, 2016, 02:10 AM
#5
Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters
you haven't said what is the problem. Have you debugged the program using the debugger - tracing through the code to see where it deviates from what expected?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 28th, 2016, 04:14 AM
#6
Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters
Originally Posted by 2kaud
you haven't said what is the problem. Have you debugged the program using the debugger - tracing through the code to see where it deviates from what expected?
The problem is that It always displays 'word accepted' even if i enter a word with no reocurring letters?
-
October 28th, 2016, 04:47 AM
#7
Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters
Code:
for ( int j= 1; i < word.Length; j++)
Don't you mean j < word.Length?
What happens when i is 1 and j is 1? The test is always equals! Shouldn't the test exclude the case j == i?
Code:
if(j != i && word.Substring(i)==word.Substring(j))
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 28th, 2016, 05:08 AM
#8
Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters
Originally Posted by 2kaud
Code:
for ( int j= 1; i < word.Length; j++)
Don't you mean j < word.Length?
What happens when i is 1 and j is 1? The test is always equals! Shouldn't the test exclude the case j == i?
Code:
if(j != i && word.Substring(i)==word.Substring(j))
So i should take out the j==1 ?and replace it with?
-
October 28th, 2016, 07:11 AM
#9
Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters
So i should take out the j==1 ?and replace it with?
Change the if test as per my post #7
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 28th, 2016, 07:43 AM
#10
Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters
Originally Posted by 2kaud
Change the if test as per my post #7
I tried that, its still saying word accepted if i only have no reoccuring letters.
could it possible be the if statement below the for loops?
-
October 28th, 2016, 07:55 AM
#11
Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters
Originally Posted by neymark
The problem is that It always displays 'word accepted' even if i enter a word with no reocurring letters?
It always says the 'word accepted' because that's what you programmed it to do. You've written 'Word Accepted' twice.
Code:
if (charCount!=0)
MessageBox.Show("Word Accepted"); // 1st time
else
MessageBox.Show("Word Accepted"); // 2nd time - oops
-
October 28th, 2016, 07:56 AM
#12
Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters
i have it changed and it still is doing it.
-
October 28th, 2016, 08:15 AM
#13
Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters
Can't give you c# code, but for c++ the code below works OK.
Code:
#include <iostream>
#include <string>
using namespace std;
int recur(const string& word)
{
int i = 0;
int charCount = 0;
for (i = 0; i < word.length(); i++)
{
for (int j = 1; j < word.length(); j++)
{
if (j != i && word[i] == word[j])
{
charCount++;
}
}
}
if (charCount)
cout << word << " accepted" << endl;
else
cout << word << " not accepted" << endl;
return charCount;
}
int main()
{
recur("qwerty");
recur("qwetqy");
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 28th, 2016, 08:23 AM
#14
Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters
Also note that using two loops has a complexity of O(n*n). By using an array and just one loop this complexity can be reduced to just O(n). Consider (again c++ - sorry)
Code:
#include <iostream>
#include <string>
using namespace std;
int recur(const string& word)
{
int cnt[256] = { 0 };
int charCount = 0;
for (int i = 0; i < word.length(); i++)
charCount += cnt[word[i]]++;
if (charCount)
cout << word << " accepted" << endl;
else
cout << word << " not accepted" << endl;
return charCount;
}
int main()
{
recur("qwerty");
recur("qwetqy");
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 28th, 2016, 08:30 AM
#15
Re: For loops and if statement to accept word to listbox, needs 2 reoccurring letters
Cheers i think ive got it working. Would you have any idea how i would go about a method accepting the word aslong as it it must start with vowel?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|