Image from accessed 5/1/17.

Strings are a little bit different from what we have seen before. They aer a 1 dimensional array and is terminated with a null character “\0” don’t forget your library <string>.

Example 1

#include <iostream>
#include <string.h>
using namespace std;//always include this when working with iostream

int main (){

string str1[10] = "Hello";

string str2[10] = "world";

string str3[10];

int len;

str3 = str1;//copy str1 into str3

cout<<"1 into 3"<<str3<<endl;

str3 = str1+str2;

cout<< "concentrate 1 and 2"<<str1<<endl;

len = str3.size();

cout<<"length of str1 ="<<len<<endl;

return 0;}

Conclusion of Example 1

The console prints out “3 into 1 hello concentrate 1 and 2 helloworld length of str1 = 10”

Strings always end in /0 so you need an extra space for it. Remember that it is a kind of pointer and handles data in the same way.

Example 2

You can also do some fun things with strings. There is more than one way to use them depending on the library you use.This can be a tricky code to read, but it can be important for future projects. Pay attention to the comments as well as the operands used.

 * int main
 * Summary:
 * First this function reads in user data to find the encrypted message.
 * Parameters: String: message (no more than 80 characters.)
 key (no more than 80 characters.)
 * Return Value: String CLE:
 * Description:
 * this function finds the encrypted message.

#include <string>
#include <iostream>
#include <cstring>
using namespace std;

int main()// ENCRYPT reads in user data and finds the encrypted message.

{string message, key, CLE;// declares 3 strings 

 cout<<"STEP 1.) ENTER ANY MESSAGE WITH ONLY CAPITAL LETTERS:"<<endl;//prompts a user command

 getline(cin,message);//reads in string user information
 int HHHHHH = message.length();//the length of the string is saved

 message[HHHHHH] = '\0';//sets the last value to /0 ending the string this is great for making a variable length string to handle data without a lot of extra steps
 if( message.length() < 81){//doesnt allow user to place more than 80 characters in message
 getline(cin,key);//takes in a Key (amount to shift the input)

 if (key.length() < 81){//doesn't let user place more than 80 characters in key
 cout<<endl<<"____________________RESULTS!____________________"<<endl<<endl<<" YOUR CRYPTED CODE IS:"<<endl;
 for(int i = 0; i < message.length(); i++)//for loop that finds the encrypted message and prints it to the screen.
if(message[i]>'Z'|| message[i]<'A'){
 message[i] = '\0'; cout<<endl<<"CAPITAL LETTERS!!!!!!!!"<<endl;}//I understand this was not a requirement. However I felt it neccessary to get rid of bad data somehow.
 else {
 CLE.push_back((((message[i] - 'A') + (key[i % key.length()] - 'A')) % 26) + 'A');//pushback goes bit by bit and assigns it to CLE. It sends every value of message through a mathmatical operation which shifts the letters based on key
 cout<<CLE[i] ;//this goes through bit by bit and prints out the string CLE which is the encrypted code
 }}else{cout<<"message too long";}
 return 0;}

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s