Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

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

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

String Encrypt(string pwords[])


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;
2 Solutions
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();                                

    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;
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);


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


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

pwords[0][0] % 10

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


>> how do i pass the array into my function


    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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now