I am a beginner at C++, (thought not really to programming in general)...........................

and decided to challenge myself to write something: A descrambler...........

I know what I need to do.
I need to:

(any code is pseudocode)
open scrambled words file
open the wordlist

:loop
get scrambled words
get wordlist
compare length of a scrambled word to all of the wordlist words
if(length of two words is the same) compare amount of each letter;<-----compare how many a's, b's, c's, etc.
if(amount of each letter is the same) print unscrambled word from wordlist to a file
goto loop(when whitespace detected)

The unscrambled wordlist is in a file, and every word is on a new line.
could i somehow detect the whitespace, and it switch down and restart the loop?

i know how to open the files, and get their contents...

what i don't know is how to find how many certain letters are in a word, and how to find the length of the word(actually, I have some idea,...just using a loop)

thanks,
Kodiak( A n00by C++ programmer)

FIXED!

//get the length of a string using the sexually transmitted library
#include <iostream>
#include <cstdlib>
using namespace std;
 
int main()
{
  string foo = "mystring";
  cout << foo.length();
}

EDIT: because i only have so long to edit i may have to double post to get some more info in thurr.

thanks alot!!! now i just need a way to compare letters...!! thanks!!!!!!!!!!!!

thanks alot!!! now i just need a way to compare letters...!! thanks!!!!!!!!!!!!

counting letters is quite easy, you can easily take a string of foo length and run a foreach on it

string foo = "some string of some length";
 
for each ( char letter in foo )
{
  cout << letter << endl;
}

i have the logic already written up for counting letters but you should play around with it some more before i hand the code over :-D

thanks, but that code didn't work.... maybe explain to me how to use it??

thanks, but that code didn't work.... maybe explain to me how to use it??

didnt work how so?

didnt work as in it wouldnt compile?

or didnt work as in it wouldnt count the letters?

make sure you are including the right files to use strings from the standard type libraries

#include <iostream>
#include <cstdlib>
using namespace std;
 
int main()
{
  string mystring = "hello world";
  for each (char letter in mystring)
  {
     cout << letter << endl;
  }
}

will output

h
e
l
l
o
 
w
o
r
l
d

AFAIK, thats not standard C++. There is no 'for...each' construct in C++. Its C++/CLI.

well damn my compiler for giving me access to for each.

let's write a while statement then!

string foo = "super duper long string of wooorrrdddsss";
int c = 0;
while ( c < foo.length() )
{
  cout << foo[c];
  c++;
}

Sorry I wasn't more specific...

I use dev C++(which I love) , and it wasn't compiling.

I then tried to compile it in visual c++ and it worked fine, but I dont think you understand exactly what I need, it's my fault, I explained my problem badly.

I need some thing that will read the word "kodiak"
and spit out:

6 characters long.
uses "k" 2 times.
uses "o" 1 time.
uses "d" 1 time.
uses "i" 1 time.
uses "a" 1 time.
...........................................If the code you posted does do this, then I'm sorry....

I don't need you to write all of my code for me, i can get the file to go to an array, and compare them, and finally write the answers to a file, etc.
I just need code that will tell me what i have posted above.

I am very grateful for you helping me become a better C++ programmer,

Kodiak

Comments
kind, polite, and willing to learn! we need more people like that in this world.

Sorry I wasn't more specific...

I use dev C++(which I love) , and it wasn't compiling.

I then tried to compile it in visual c++ and it worked fine, but I dont think you understand exactly what I need, it's my fault, I explained my problem badly.

I need some thing that will read the word "kodiak"
and spit out:

6 characters long.
uses "k" 2 times.
uses "o" 1 time.
uses "d" 1 time.
uses "i" 1 time.
uses "a" 1 time.
...........................................If the code you posted does do this, then I'm sorry....

I don't need you to write all of my code for me, i can get the file to go to an array, and compare them, and finally write the answers to a file, etc.
I just need code that will tell me what i have posted above.

I am very grateful for you helping me become a better C++ programmer,

Kodiak

awesome you are on the right track

so if we have a statement to itterate through all of the letters in the word it is now about counting each of the letters as they appear :)

case/switch will work for this

psuedo code:

while counting through each letter in the word

check to see what letter you are

check to see if you are a letter (not a space or symbol)

if you are a letter determin which letter you are

once i have determined the letter add you to my letter counter

etc...do this for each letter in the word

can you explain what foo is to me?
what will i have to do to write the pseudocode in c++?

what function can i use to help me find one letter, and read it's value?? this is all sort've confusing... I thought the basic file i/o system was complicated when i learned it..

well damn my compiler for giving me access to for each.

let's write a while statement then!

string foo = "super duper long string of wooorrrdddsss";
int c = 0;
while ( c < foo.length() )
{
  cout << foo[c];
  c++;
}

Computationally expensive. length() function is called each time the loop is executed. Why not something simple like:

int length = foo.length();
for(int i = 0; i < length; ++i)
   cout << foo[i] << '\n';

can you explain what foo is to me?
what will i have to do to write the pseudocode in c++?

what function can i use to help me find one letter, and read it's value?? this is all sort've confusing... I thought the basic file i/o system was complicated when i learned it..

'foo' and sometimes 'bar' are just used in psuedo code or examples instead of using actual names because the example given may have no actual context :)

'foo' could be replaced with 'realygigantvariablename' or even 'ilikepickles'

'foo' is like the variable x in math, just a place holder :)

to write psuedo code? nothing, just get wordpad and write it, psuedo code is just so you can get the basic idea of what you are doing without having to open up your compiler and waste time.

it is a good method to plan out your ideas.

no particular function.

lets say i've got the word "robertson"

i want to know how many o's are in the word...i could do this!

//assume standard includes ive used above
 
int main()
{
    string name = "Robertson";  
    int c = 0, //counter for the while  
        cMax = int (name.length()), //the length of the name so we can cache it  
        oCount = 0; //the number of o's found in the word   
    while ( c < cMax )  
    {    
        if ( char (towlower(name[c])) == 'o' ) //change the letter to lowercase and check to see if it equal to the letter o       
            oCount++; // if it is incriment by one    
        c++; //incriment our counter  
    }
 
}

okay so a line-by-line look:
line 5: i am declaring a string with the intial value of "Robertson"
line 6-8:
On these lines i am setting up the counters and maximum for the while statement counter.
the variable 'c' is being used to count through the length of the variable name while cMax is the total number of letters in the variable name.
oCount is being used to count the number of o's in the word given
line 10: starting the while statement while c (less than)< cMax do the following
line 12: if the character at the given point in the string is the letter o
the first part

char (towlower(name[c]))

is pretty simple, i am "casting" the return from the function to character (due to the return type of towlower being wint_t
the function towlower transforms the given character to a lowercase character. some compilers may use tolower instead but the visual studio 2005 version/CLI is using towlower.
line 13: add one to our counter if it is true


------------------

Computationally expensive. length() function is called each time the loop is executed. Why not something simple like:

int length = foo.length();
for(int i = 0; i < length; ++i)
   cout << foo[i] << '\n';

hehe see this post, i cached it in this version, not the last i was in a rush :-X

This article has been dead for over six months. Start a new discussion instead.