Encoder?

I'm trying to write a program that encodes the user's input. It is a simple three-letter shift. This is what I have so far:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;

string input;

string encode_input(input)
{
	char switched_alphabet[25]=["d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","a","b","c"];
	char alphabet[25]=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
}	
int main (int nNumberofArgs, char* pszArg[]) 
{
	cout<<"Enter the input you would like to encode:";
	cin>>input;
	encode_input(input);
	
return 0;
}


How can I search the user's input, then shift it three letters? Is there a quick, efficient way to do it?

Happy commenting!
Last edited on
Replace s with the name of your string.
1
2
3
4
for(int i=0;i<s.length();i++)if(isalpha(s[i])){
	int shift=islower(s[i])?'a':'A';
	s[i]=(s[i]-shift+3)%26+shift;
}

Does this do what you're looking for?
Last edited on
Topic archived. No new replies allowed.