0
#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.

Edited by Dani: Fixed formatting

2
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by tom12
0

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

0

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

Edited by deceptikon: n/a

0

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

It's all good Thanks.

0

It's all good Thanks.

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

0

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().

0

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 .

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.