passing a word into a function

the user enters a sentence "thankgiving is on thursday"
pwords[0] = "thanksgiving"
pwords[1] = "is"
pwords[2] = "on"
pwords[3] = "thursday"

how do i pass the array into my function

String Encrypt(string pwords[]) ??maybe

and how do i break eachword down into individaul characters so i can do operations on them

so i want it to take thanksgiving and go though
and get the t then the h than the a and so on.......

also can i do mathmatical operations on the letters
say i take the varible that stores t and mod 10 is that going to deal with the ascii representation and will it be legal??

hopefully you can understand what i am trying to get at
thanks in advance
ted

heres what i am worken with
even though it is probbly way out of wack

String Encrypt(string pwords[])

letter

Number1 = letter % 10;
Number1 = (Number1 + 7) % 10;

Number2 = letter / 10;
Number2 = (Number2 + 7) % 10;

EncryptedLetter = static_cast<char>(Number1) + static_cast<char>(Number2);

return EncryptedLetter;
tedschniedersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tedschniedersAuthor Commented:
the other part of my code to get the words into the array is this

#include <iostream>
using namespace std;

void Crypt(string &pwords);

int main() {
 
string text;                                            
const string separators = " ";            
int Answer = 0;

cout << endl << "Please Enter A Sentence:" << endl;
std::getline(cin, text);

cout << endl << "What do you wish to perform on this Senetence. \n" << endl;
cout << "Press 1) To Decrypt The Sentence." << endl;
cout << "Press 2) To Encrypt The Sentence." << endl;
cin  >> Answer;



size_t start = text.find_first_not_of(separators);      
size_t end = 0;                                      
int word_count = 0;                                    

while(start != string::npos){
   end = text.find_first_of(separators, start + 1);
if(end == string::npos)                              
      end = text.length();                                
    word_count++;                                        


    start = text.find_first_not_of(separators, end + 1);
}


string** pwords = new string*[word_count];

start = text.find_first_not_of(separators);            
end = 0;                                              
int index = 0;                                          

while(start != string::npos){
  end = text.find_first_of(separators, start + 1);
  if(end == string::npos)                              
    end = text.length();                                
pwords[index++] = new string(text.substr(start, end - start));  
   

start = text.find_first_not_of(separators, end + 1);
}





for(int i = 0 ; i < word_count ; i++)
  delete pwords[i];


delete [] pwords;

cout << endl;

      return 0;
}
0
efnCommented:
You could use a vector of strings.

vector<string> pwords;

Or you could use a dynamically allocated array of strings.

string* pwords = new string[word_count];

Your current design of having a dynamically allocated array of dynamically allocated strings is unnecessarily complex and doesn't really buy you anything.

Pass it to a function by reference.

void func(vector<string>& vecstr);

func(pwords);

You can address individual characters of a string with the [] operator, just like an array.

pwords[0][0]

Characters are integral, so you can do arithmetic with them.

pwords[0][0] % 10

--efn
0
SirHandoCommented:
You pass a pointer, and use a terminator to indicate no more in the array.

void func( string* words )
{
  int i=0;
 
  while ( words[i] != NULL )
  {
    // Do something with words[i] here.
   
    // Next word
    ++i;
  }  
}

Ciao

SirHando
0
itsmeandnobodyelseCommented:
>> how do i pass the array into my function

Yes,

    string Encrypt(string pwords[]) ;

would be a usual way, or better

    string Encrypt(string pwords[], int numStrings);

With that you could pass the size of the array to Encrypt function. Or - maybe best - use std::vector<strings> and pass it by reference as efn had suggested.

>> how do i break eachword down into individaul characters

   string pwords[10];
   pwords[0] = "thanksgiving"
   pwords[1] = "is"
   pwords[2] = "on"
   pwords[3] = "thursday"

   string encrypted = Encrypt(pwords, 4);

   string Encrypt(string pwords[], int numStrings)
   {
        string weird = "/&@,._:|<>+~#'-][}{";
        int     len = weird.length();
        string encrypted;
        for (int i = 0; i < numStrings; ++i)
             for (int j = 0; j < pwords[i].length(); ++j)
             {
                   unsigned char c  = pwords[i][j];   // get j-th char
                   c ^= weird[(i*j)%len];
                   encrypted += c;
             }    
        return encrypted;
   }

Note, type 'unsigned char' could be used as type 'int_8' , a 8 bit integer that runs from 0 to 255, where space = 32 and 'A' = 65 for example.

Regards, Alex
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.