Ignoring the Space

closed account (21vXjE8b)
Yeah, I'm back with another question.
My code works like this: You write a message (heLLo), choose a key value (e.g. 3) and the result is khOOr (3 letters forward, with lower or upper case!).
The problem, is that if you write "Hello World" (with key value = 3), it will print "Khoor#Zruog". That means the space was considered in the code and was codified. I wanna ignore it, but maintaining he in the final.

Original code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 string msg_E;
    char result_E, i;
     int i;

	for (i=0; i < msg_E.length(); i++) {
			result_E = msg_E[i] + keyValue_E;
			
			if (isupper(msg_E[i])) {
				if (result_E > 90) result_E -= 26;
			}
		
			if (islower(msg_E[i])) {
				if (result_E > 122) result_E -= 26;
			}
			
			cout << result_E;
		}



I tried to restrict the ASCII characters, but it didn't work.
*In here, even if result_E = msg_E[i] + keyValue_E; is inside or before the If statements, the space continue being considered.

1
2
3
4
5
6
7
8
9
10
11
12
for (i=0; i < msg_E.length(); i++) {
			result_E = msg_E[i] + keyValue_E;
		
			if (msg_E[i] >= 65 && msg_E[i] <= 90) {
				if (result_E > 90) result_E -= 26;
			}
			
			if (msg_E[i] >= 97 && msg_E[i] <= 122) {
				if (result_E > 122) result_E -= 26;

                        cout << result_E;
			}


And I tried to consider only alphabetic characters, but it suppress the space in the end.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
     for (i=0; i < msg_E.length(); i++) {

          result_E = msg_E[i] + keyValue_E;

          if (isalpha(msg_E[i])) {
			
			
			if (isupper(msg_E[i])) {
				if (result_E > 90) result_E -= 26;
			}
		
			if (islower(msg_E[i])) {
				if (result_E > 122) result_E -= 26;
			}
			
			cout << result_E;
		        }
		}

Last edited on
Hi,
> I tried to restrict the ASCII characters, but it didn't work.
What didn't work?
closed account (21vXjE8b)
The space continue being considered.
So you want to keep the space or you want to remove the space?
closed account (21vXjE8b)
I want to keep the space.
1
2
3
for (i=0; i < msg_E.length(); i++)
{
     if(isspace(msg_E[i])) continue;


closed account (21vXjE8b)
Didn't work... =/
It suppressed the space in the result.
> Didn't work... =/
It suppressed the space in the result.

What do you mean by that? What happened to the previous statement of yours?

> The space continue being considered.
closed account (21vXjE8b)
Sorry, I wasn't clear... :S
In the original code, if I enter "Hello World", it prints "Khoor#Zruog".
With the isspace, if I enter "Hello World", it prints "KhoorZruog".
I want it to prints "Khoor Zruog".
I assume you meant this?
1
2
3
for (i=0; i < msg_E.length(); i++)
{
     if(isspace(msg_E[i])) {cout << msg_E[i]; continue;}


Does that help you? :)
closed account (21vXjE8b)
Oh my... Thank you so much!!! =D
Glad it helped :)
Topic archived. No new replies allowed.