Char Array

Pages: 123
Hello everyone

I need help with this question? can anybody tell me how to start?

I wrote the first thing but then I couldn't proceed..

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string wordToEncode;

	cout << "Please enter the word that you want to encode" << endl;
	cin >> wordToEncode


}


In cryptography, Caesar cipher is one of the simplest encryption techniques. The key idea of this method is to replace each plaintext letter with one fixed number of places down the alphabet. Below is an example with a shift of three:

Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Cipher: DEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABC

To cipher a string, ‘A’ is replaced by ‘D’, ‘B’ is substituted by ‘E’, and so on. To decode a string, ‘A’ is replaced by ‘x’, etc.

By using the Caesar cipher with a shift of three, write a program that first ask user to input a word (such as Student). Then, your code will print out the encoded word for the input (i.e., Vwxghqw for Student. Next, your code asks users to input a ciphered code (such as Vwxghqw), and a decoded word will be finally printed out.

Test your program with a word: “Student”. Demonstrate the encoded word and the deciphering of the encoded word.

Hint: the length of a char array can be determined by a built-in function: strlen(char_array_name), which returns an integer that is the length of the char array.
closed account (3qX21hU5)
Are you having trouble with how to implement a Caesar Cipher? Why not google it? Here is a link that might help which is published by our very own catfish ;p - http://www.cplusplus.com/articles/9hvU7k9E/ check it out it has some very good information in it.


Also here is a very simple sample of shifting a string 3 letters forward. It should get you on the right track.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

const int shift = 3;

int main ()
{
    string test("abc");

    for (string::size_type i = 0; i != test.size(); ++i)
    {
        test[i] += shift;
    }

    cout << test << endl;
    return 0;
}

Last edited on
closed account (Dy7SLyTq)
replace it with a c string and step through it
closed account (3qX21hU5)
Why on earth would you replace it with a c string? Strings you can step through just the same as you can a c string, and are so much easier to use and are less error prone.

The only reason you should have to use a c string is when your professor requires you to.
closed account (Dy7SLyTq)
your telling me that a class that you have limited access to is going be less error prone that simple char * ops?

1
2
3
4
5
6
7
char *code = "ABC";
const int shift = 3;

for(int i = 0; i < strlen(code); ++i)
{
     code[i] += shift;
}
closed account (Dy7SLyTq)
i feel like the shift wont work in this case but i cant remember the proper way
its like 'A' - code[i] + 3 i havent done that in a while
closed account (3qX21hU5)
Yes that is exactly what I am telling you. Strings are FAR less error prone then char * are and that is common knowledge I thought. The most common errors leading to errors when it comes to c style strings are unbounded string copies, null- termination errors, and string truncation. Also it makes absolutely no sense to convert a string back into a c style string just so you can do something that a string already supports. You are adding a extra step in there that has no place being there.
closed account (Dy7SLyTq)
...... .................... ................................ ... ........ its the other way around... ive gotten plenty of errors with the string class before. the only time i got errors with char *'s was when i was treating them like the string class
closed account (3qX21hU5)
Sigh guess we should just get rid of the string class since it is worse then c strings ;p . Keep doing what works for you everyone is different but if you ask 99% of programmers they will most likely tell you strings are MUCH better to use then c strings.



1
2
3
4
5
6
7
char *code = "ABC";
const int shift = 3;

for(int i = 0; i < strlen(code); ++i)
{
     code[i] += shift;
}

^
|
By the way your example doesn't work so yes c strings are inferior ;p
Last edited on
closed account (Dy7SLyTq)
ok i agree that vectors are greater than arrays. as to the above, that is because they are taking the easy way out. now ill admit i use the string class, but thats only when i need a quick and simple fix. such as using system. only in circumstances when i dont have time for a better solution until later
closed account (3qX21hU5)
as to the above, that is because they are taking the easy way out.


What? So you are saying if something is better and easier to use, programmers are basically slacking off because they use it instead of something that is much worse and harder to use? That is like saying instead of using a hammer to pound in nails I'll just use my head.
closed account (Dy7SLyTq)
no im saying that lets use a hammer and nail instead of a nail gun, which has a higher chance of malfunctioning, needs a battery to be charged, and doesnt give me the same satisfaction as doing it with a regular nail and hammer
closed account (3qX21hU5)
Can you give me a instance where a string can "malfunction" and a c string wont? Also can you list some added functionality that a c string has that strings don't?

I am not trying to be rude I just trying to understand why you think a string is more error prone then a c string and how you think they are easier to use then strings.
closed account (Dy7SLyTq)
i understand. were r just having a debate. i never said a c-string was more functional, just that you have more control. and you mis understood me or i mistyped. ahhhh typing this i see what the problem is, well maybe. so i couldnt get a c++ compiler working. so i used c instead. when i got back into c++, strings were very high level and thus, not used right unless i looked something up.
closed account (3qX21hU5)
I also fail to see where you have more control over a c string then you do on a string.

strings were very high level and thus, not used right unless i looked something up.


Strings are not very high level really, they are not some multi abstracted class or anything. They can do everything a c string can do and more. I think the problem is you don't know enough about strings and how they work and that is why you get more errors with them.

closed account (Dy7SLyTq)
what do you think a string is? at its base its a char *. i was getting errors if i used it as a char *. anything you can do with a string you can do with a char*
what do you think a string is? at its base its a char *. i was getting errors if i used it as a char *. anything you can do with a string you can do with a char*


Show me code equivalent to the following using "char*."


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>

std::string func()
{

    std::cout << "Enter a string of any length.\n" ;
    std::string a ;
    std::getline(std::cin, a) ;

    std::cout << "Enter another string of any length.\n" ;
    std::string b ;
    std::getline(std::cin, b) ;

    return a+ ' ' + b ;
}

int main()
{
    std::string result = func() ;
    std::cout << "Concatenated: \"" << result << "\"\n" ;

    std::size_t pos ;
    while ( (pos = result.find_first_of( "aeiouAEIOU" )) != std::string::npos )
        result.erase(pos,1) ;

    std::cout << "With all vowels removed: " << result ;
}
closed account (Dy7SLyTq)
i am at work so i dont have my ide to make sure it works fine i need to wait until i get home
@DTSCode,
http://www.cplusplus.com/reference/string/string/data/

std::strings are superior in every way.
closed account (Dy7SLyTq)
ur link just proves that they are at the least the same
Pages: 123