CHAR* AND CONST CHAR* INCOMPATABILITY

Write your question here.
How do you change cstr, which is a char* to a const char* so that atoi can work?

1
2
3
4
5
  char* cstr = "This is C++."
  int key = 2;

  int cUp = (atoi(cstr[0]) + key) % 26;
I don't think you need a atoi here. Also, a couple of points:
1. "This is NOT C++" :)
2. You are missing a semicolon here.

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>

int main(void) {
    char* cstr = "This is C++.";
    int key = 2;

    int cUp = ((cstr[0]) + key) % 26;
    printf("cUP: %d\n",cUp);
	
    return 0;
}


Output:
Success	time: 0 memory: 2248 signal:0
cUP: 8

Last edited on
Sorry, the assignment was to use a string. Here is my code. I am stuck. I do not know why it aborts after the 'h' in "This is C++!". I am not finished yet. This is my draft.


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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
*	ceasar
*
*	Get the key
*	Get the plaintext 
*	Encipher the text
		One character
		Entire plaintext
		isapha
		isupper
		islower
*	Print cipher text message
*	Start: ASCII values
*	Encipher: Alphabetical index
*	Print: ASCII values
*
*	c[i] = (p[i] + k) % 26
*
*/

#include <cstdio>	// printf_s scanf_s
#include <cstdlib>  // atoi
#include <cctype>	// isupper, islower
#include <iostream> // cin, cout
#include <string>	// getline

using namespace std;

int main(int argc, char* argv[])
{
	// error check
	if (argc != 2)
	{
		printf_s("Illegal Usage: ./ascimath key");
		return 1;
	}
	// key is the second command line argument and is converted from char to integer
	int key = atoi(argv[1]);
	// variable
	string cstr ("This is C++!");
	string dest;
	int incr = 0;

	// Get message from user
//	printf_s("Enter the message: ");
//	printf_s("%i\n", cstr);
//	getline(cin, cstr);
	int len = cstr.length();
	printf_s("len is: %i\n", len);

	while (cstr[incr])
	{
		cout << "working character: " << cstr[incr] << endl;
		// encrypt message

		if (isupper(cstr[incr]) != 0)
		{
			// change ASCII value to encrypt value
			int x  = (cstr[incr] + key) % 26;
			printf_s("x in isupper is: %i\n", x);
			dest[incr] = x;
		}
		else if (islower(cstr[incr]) != 0)
		{
			int x = (cstr[incr] + key) % 26;
			printf_s("x in islower is: %i\n", x);
			dest[incr] = x;
		}
		else
		{
			dest[incr] = cstr[incr];
		}
		incr++;
		printf_s("incr is %i\n", incr);
	}
	// Print encrytion.
	for (int i = 0; i < len; i++)
	{
		printf_s("%c", dest[i]);
	}
}








In your calculations, subtract 'a' or 'A' from cstr [incr] respectively for lower and uppercase. Then add it back while assigning to dest [incr].

Also, termination condition should be while incr < len.
abhiskehim71,

I made the change to the Boolean expression in the while statement. I subtracted 'A' and 'a' from cstr[incr] in the upper and lower case if statements, respectively I did not add them back because I am not ready to decipher the string.

I am still getting a Debug Assertion Failure: string substring out or range.

I don't know what your printf_s function is, but the following works for me:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
*	ceasar
*
*	Get the key
*	Get the plaintext 
*	Encipher the text
		One character
		Entire plaintext
		isapha
		isupper
		islower
*	Print cipher text message
*	Start: ASCII values
*	Encipher: Alphabetical index
*	Print: ASCII values
*
*	c[i] = (p[i] + k) % 26
*
*/

#include <cstdio>	// printf_s scanf_s
#include <cstdlib>  // atoi
#include <cctype>	// isupper, islower
#include <iostream> // cin, cout
#include <string>	// getline

using namespace std;

int main(int argc, char* argv[])
{
	// error check
	if (argc != 2)
	{
		printf("Illegal Usage: ./ascimath key");
		return 1;
	}
	// key is the second command line argument and is converted from char to integer
	int key = atoi(argv[1]);
	// variable
	string cstr ("This is C++!");
	string dest( cstr.size(),'x' );
	int incr = 0;

	// Get message from user
//	printf_s("Enter the message: ");
//	printf_s("%i\n", cstr);
//	getline(cin, cstr);
	int len = cstr.length();
	printf("len is: %i\n", len);

	while (incr < len)
	{
		cout << "working character: " << cstr[incr] << endl;
		// encrypt message

		if (isupper(cstr[incr]) != 0)
		{
			// change ASCII value to encrypt value
			int x  = (cstr[incr] - 'A' + key) % 26;
			printf("x in isupper is: %i\n", x);
			dest[incr] = x + 'A';
		}
		else if (islower(cstr[incr]) != 0)
		{
			int x = (cstr[incr] - 'a' + key) % 26;
			printf("x in islower is: %i\n", x);
			dest[incr] = x + 'a';
		}
		else
		{
			dest[incr] = cstr[incr];
		}
		incr++;
		printf("incr is %i\n", incr);
	}
	// Print encrytion.
	for (int i = 0; i < len; i++)
	{
		printf("%c", dest[i]);
	}
}


Output in Linux UBUNTU:
abhishekm71@PC:~$ ./temp 2
len is: 12
working character: T
x in isupper is: 21
incr is 1
working character: h
x in islower is: 9
incr is 2
working character: i
x in islower is: 10
incr is 3
working character: s
x in islower is: 20
incr is 4
working character:  
incr is 5
working character: i
x in islower is: 10
incr is 6
working character: s
x in islower is: 20
incr is 7
working character:  
incr is 8
working character: C
x in isupper is: 4
incr is 9
working character: +
incr is 10
working character: +
incr is 11
working character: !
incr is 12
Vjku ku E++!


EDIT: dest string initialized to match the length of cstr.
Last edited on
abhishekm71

I copied your code and pasted it into Visual Studio 2013 Express and got the same Debug Assertion Failure. I have revised my code since then and will post it under a different subject.

Thank you for your time.
Topic archived. No new replies allowed.