#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    char clear[200];
    char cipher[200];
    int x,i;


    cout<<" Enter a string:";
    cin.getline(clear,sizeof(clear));

    x = strlen(clear);

    for(i=0;i<=x-1;i++)
    {
    cipher[i] = clear[i]+3;
    }
    cipher[x] = '\0';
    cout<<" Encrypted:" << cipher << endl;

    system("pause");

    return 0;
}

Hey guys hope someone could help me, as i'm new to programming. i need to encrypt a full sentence. with my program above it will encrypt but as soon as it reaches a space it will output only one word and not the full sentence. how can i encrypt the full sentence.
thanks guys hope you can help. im guessing as soon the program see's a spaces by default its see's '\0' is this correct.

I can't reproduce your problem, it encrypts the whole sentence for me. What compiler and OS are you using?

I can't reproduce your problem, it encrypts the whole sentence for me. What compiler and OS are you using?

DevC++ and windows 7. yes I'm getting "#" were spaces are. Thanks for replying

So is the problem solved, or did you want to preserve whitespace and only encrypt non-whitespace characters?

So is the problem solved, or did you want to preserve whitespace and only encrypt non-whitespace characters?

It's all good Thanks.

It's all good Thanks.

If i did want to preserve the white-space how would i go about doing this.

You'd just filter out whitespace by assigning clear as-is instead of shifting the character:

for(i=0; i<=x-1; i++)
{
    if (isspace(clear[i]))
    {
        cipher[i] = clear[i];
    }
    else
    {
        cipher[i] = clear[i]+3;
    }
}

Be sure to include <cctype> or <ctype.h> for the declaration of isspace().

You'd just filter out whitespace by assigning clear as-is instead of shifting the character:

for(i=0; i<=x-1; i++)
{
    if (isspace(clear[i]))
    {
        cipher[i] = clear[i];
    }
    else
    {
        cipher[i] = clear[i]+3;
    }
}

Be sure to include <cctype> or <ctype.h> for the declaration of isspace().

Thanks for the help Deceptikon much appreciated .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.