passing a word into a function

Posted on 2004-11-21
Last Modified: 2012-05-05
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;
Question by:tedschnieders

    Author Comment

    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;
    LVL 15

    Assisted Solution

    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

    LVL 1

    Expert Comment

    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


    LVL 39

    Accepted Solution

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

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now