Char outputting random characters at the end of the sentence - c++

#include <iostream>
#include <string.h>
using namespace std;
void crypt(char* sMsg)
{
cout << "Original Message: '" << sMsg << "'" << endl;
int length = strlen(sMsg);
char sMsg_Crypt[3][length];
/* sMsg_Cryp[3]
[0] CRYPT LETTERS, ASCII + 3
[1] INVERT CHAR
[2] HALF+ OF SENTENCE, ASCII - 1
*/
for (int i=0; i<length; i++)
{
if (isalpha((int)sMsg[i]))
sMsg_Crypt[0][i] = sMsg[i] + 3; // DO ASCII + 3
else
sMsg_Crypt[0][i] = sMsg[i];
}
cout << "Crypt[0]: '" << sMsg_Crypt[0] << "'" << endl;
}
int main()
{
char sMsg[256];
cin.getline(sMsg,256);
crypt(sMsg);
return 0;
}
Input:
Hello World! Testing the Cryptography...
Output:
Original Message: 'Hello World! Testing the Cryptography...'
Crypt[0]: 'Khoor Zruog! Whvwlqj wkh Fu|swrjudsk|...Çi­o'
Why this Çi­o is comming out??

For starters variable length arrays like this
int length = strlen(sMsg);
char sMsg_Crypt[3][length];
is not a standard C++ feature.
You could use at least an array of objects of the type std::string like for example
std::string sMsg_Crypt[3];
Nevertheless the problem is that the array sMsg_Crypt[0] dies not contain a string. That is you forgot to append inserted characters in the array with the terminating zero character '\0'.
You could write after the for loop
sMsg_Crypt[0][length] = '\0';
provided that the array (if the compiler supports VLA) is declared like
char sMsg_Crypt[3][length+1];

Firstly, you can't define a static char array like this: char sMsg_Crypt[3][length];. That is because the length is not a const type, meaning the size of the array will be sMsg_Crypt[3][0] (this is because the size is not known at compile time). In MSVC, it'll flag an error (by IntelliSense). Since you know the size beforehand (256), you can replace the length with 256.
The second fact is that you're using C++ and you have access to std::string. So without using a char buffer, use std::string instead. It would look something like this: std::string sMsg_Crypt[3];
The last fact is that, for a string to be read correctly, it needs to be null-terminated ('\0' at the end). This means that the ending character must be '\0'. In the case of std::string, it does it for you.

Related

Are std::string with null-character possible?

I initialized a C++ string with a string literal and replaced a char with NULL.
When printed with cout << the full string is printed and the NULL char prints as blank.
When printed as c_str the string print stop at the NULL char as expected.
I'm a little confused. Does the action came from cout? or string?
int main(){
std::string a("ab0cd");
a[2] = '\0'; // '\0' is null char
std::cout << a << std::endl; // abcd
std::cout << a.c_str() << std::endl; // ab
}
Test it online.
I'm not sure whether the environment is related, anyway, I work with VSCode in Windows 10
First you can narrow down your program to the following:
#include <iostream>
#include <string>
int main(){
std::string a("ab0cd");
a[2] = '\0'; // replace '0' with '\0' (same result as NULL, just cleaner)
std::cout << a << "->" << a.c_str();
}
This prints
abcd->ab
That's because the length of a std::string is known. So it will print all of it's characters and not stop when encountering the null-character. The null-character '\0' (which is equivalent to the value of NULL [both have a value of 0, with different types]), is not printable, so you see only 4 characters. (But this depends on the terminal you use, some might print a placeholder instead)
A const char* represents (usually) a null-terminated string. So when printing a const char* it's length is not known and characters are printed until a null-character is encountered.
Contrary to what you seem to think, C++ string are not null terminated.
The difference in behavior came from the << operator overloads.
This code:
cout << a.c_str(); // a.c_str() is char*
As explained here, use the << overloads that came with cout, it print a char array C style and stop at the first null char. (the char array should be null terminated).
This code:
cout << a; // a is string
As explained here, use the << overloads that came with string, it print a string object that internally known is length and accept null char.
string end limit (boundary) is not 0 (NULL) like simple char* but its size keep internally in its member data as it's actually user-defined type (an instantiated object) as opposed to primitive type, so
int main(){
string a("abc0d");
a[3] = 0; // '\0' is null char
a.resize(2);
std::cout << a << std::endl; // ab
std::cout << a.c_str() << std::endl; // ab
}
i'm sorry change your code to be more comfortable, watch as it results in
ab
ab
good learning: http://www.cplusplus.com/reference/string/string/find/index.html

Converting input from lowercase to uppercase using ASCII codes in C++

This is my first question here, so I've done my best to make this a good question.
I am creating a program that essentially takes user input and converts all characters to uppercase. I am using a for-loop to scan for lowercase characters using corresponding ASCII codes.
I am able to do this just fine using a character array that is assigned a string in-code: char text[] = "Text".
I want to be able to take user input and use it in the character array. I tried using getline(cin,myString) and assigning the character array with that, but it says array must be initialized with a brace enclosed initializer.
I kept the character array uninitialized because sizeof(text) isn't giving the correct size when the array is initialized. I was reading about using pointers but I'm still a bit fresh on that topic. Below is the code I wrote:
int main() {
// User input as a string
char textConvert[] = "This text will be converted to uppercase.";
cout << textConvert << endl;
int endChar = sizeof(textConvert); //Only gives correct size when array is uninitialized
for (int i = 0; i < endChar; i++) {
if (textConvert[i] >= 97 && textConvert[i] <= 122) {
textConvert[i] = textConvert[i] - 32;
}
}
cout << textConvert;
return 0;
}
Question:
I tried using getline(cin,myString) and assigning the character array with that, but it says array must be initialized with a brace enclosed initializer
Here the compiler works out the size of the array needed.
char textConvert[] = "This text will be converted to uppercase.";
If you want user input you need to allocate an array and specify size.
char textConvert[50];
Now you can read a line and copy it into the array:
std::string myString;
std::getline(std::cin , myString);
// Should check that the string is not more than 50 characters.
std::copy(std::begin(myString), std::end(myString), textConvert);
But really there is no need to do this at all. Just use the std::string and loop over the string. Best to avoid C constructs like arrays and use the C++ constructs that stop you making errors.
Size of String
This is not a good idea.
int endChar = sizeof(textConvert);
This measures the size of the array (not the size of the string). There is also an issue that arrays will very easily decay into pointers. When this happens sizeof() will give you the size of the pointer (probably 4 or 8) not the size of the array.
To get the size of a string use std::strlen() (include <cstring>).
But really you should be using std::string the C++ version of string that does its own memory management and re-sizes as required.
Magic Numbers
Prefer not to use magic numbers:
if (textConvert[i] >= 97 && textConvert[i] <= 122) {
textConvert[i] = textConvert[i] - 32;
}
These magic numbers make the code hard to read. You can use character constants instead.
if (textConvert[i] >= 'a' && textConvert[i] <= 'z') {
textConvert[i] = textConvert[i] - ('a' - 'A');
}
Prefer the standard Library
But doing this manually is not recommended. You should use the standard library routines.
std::islower() . // Check if a character is lower case.
std::toupper() . // Convert a lowercase character to upper.
// include <cctype>
C++ Example
Try this:
#include <iostream>
#include <string>
#include <cctype>
int main()
{
std::string myString;
while(std::getline(std::cin, myString)) {
std::cout << "User Input: " << myString << "\n";
for(auto& c: myString) {
c = std::toupper(c);
}
std::cout << "Upper Case: " << myString << "\n";
}
}
Since you are dealing with ASCII, you can just use std::toupper.
No need to write custom code to do it, the standard library has you covered.

Pointer to a C-String? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
For an intro to CS assignment, I am writing a C++ program in Visual Studio 2010 which returns an integer and accepts a pointer to a C-string as an argument. I know that I need to make a function besides an int main to succeed, but I am not exactly sure about how to initialize a char pointer array which points to a predefined char array, if possible.
The purpose of this program is to get a comment from a user within a predefined limit then inform said user how many characters (including whitespace) that comment is.
The error is: a value of type "char" cannot be assigned to an entity of type "char *";
Here is my program that does not compile:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
//function protoype
void evalStr(char arr[]);
//variable
int length;
//main function
int main()
{
const int SIZE = 201;
char arr[SIZE];
char *str[SIZE];
cout << "Please enter a comment under " << (SIZE - 1) << " characters to calculate it's length: ";
*str = arr[SIZE];
cin.getline(arr, SIZE);
length = strlen(*str);
evalStr(arr);
system("PAUSE");
return 0;
}
//function defintion
/* get the string
count the number of characters in the string
return that number
*/
void evalStr(char arr[])
{
printf("The length of the entered comment is %d characters\n", length);
}
If there is a general method to utilizing char pointer arrays or perhaps pointers to strings, this code could be redone to return the value of the string instead of utilizing that printf statement. What all am I doing wrong?
Edit: Here is an updated version of this program that compiles, runs, and informs the user if the character limit is reached or exceeded.
// Accept a pointer to a C-string as an argument
// Utilize the length of C-string in a function.
// Return the value of the length
// Display that value in a cout statement.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
//variables
const int SIZE = 201;
int length;
char arr[SIZE];
char *str;
//main function
int main()
{
str = &arr[0];
// Size - 1 to leave room for the NULL character
cout << "Please enter a comment under " << (SIZE - 1) << " characters to calculate it's length: ";
cin.getline(arr, SIZE);
length = strlen(str);
if (length == (SIZE - 1))
{
cout << "Your statement has reached or exceeded the maximum value of "
<< length << " characters long.\n";
}
else
{
cout << "Your statement is ";
cout << length << " characters long.\n";
}
system("PAUSE");
return 0;
}
//function defintion
/* get the string
count the number of characters in the string
return that number
*/
int countChars(int)
{
length = strlen(str);
return length;
}
Let's talk about what is happening in main instead:
int main()
{
Okay, so you're going to have strings of 201 characters. Seems reasonable.
const int SIZE = 201;
And you've declared an array of 201 characters:
char arr[SIZE];
And now you declare an array of 201 pointers to characters. I'm not sure why you would want to do that. I suspect you think this does something other than what it actually does:
char *str[SIZE];
This is reasonable (except "it's" means "it is", but you want the possessive version, "its"):
cout << "Please enter a comment under " << (SIZE - 1) << " characters to calculate it's length: ";
This assigns the 201st character to the first character pointer in your array of char pointers. This is an error because:
arrays are zero-indexed, so the 201st character (when you start counting at zero) is beyond the end of the array.
you haven't initialized the memory in arr to anything yet.
you're assigning a char to a char *.
So, given the above I'm not sure why you're doing this:
*str = arr[SIZE];
This looks reasonable:
cin.getline(arr, SIZE);
This is an error because str doesn't point to memory that contains a valid string at this point.
length = strlen(*str);
When you "point to a string", actually you point to the first character of the string. The end of the string can be found by looking at successive memory locations until you find a location containing a null byte. So your code should be:
char *str; // can point at a character
// ....
str = &arr[0]; // point to the first character of arr
// ....
length = strlen(str);

getting the substring that does not contain the first character in the string

If the string was abcdef how do I get bcdef. I tried
cout << str.substr(1,'\0')<< endl;
but that didn't work.
What about a char array?
#include <iostream>
using namespace std;
int main() {
std::string str = "abcdef";
cout << str.substr (1);
return 0;
}
Try it live: http://ideone.com/AueOXR
if you take a look at the documentation for string::substr:
string substr (size_t pos = 0, size_t len = npos) const;
Generate substring
Returns a newly constructed string object with its value initialized to a copy of a substring of this object.
The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first).
http://www.cplusplus.com/reference/string/string/substr/
That means: if you don't specify a second parameter a default one that goes all the way to the end of the string is assumed.
Try
cout << str.substr(1)<< endl;
Of course, the documentation of string tells you everything you need to know ...
Make it simpler:)
std::cout << str.substr( 1 ) << std::endl;
The other way and I think more effective is to use member function c_str if the string does not contain embedded zeroes
std::cout << str.c_str() + 1 << std::endl;
substr(position, length) will return the string started at the position index and ending at length characters after position.
You need to change the \0 to either the end of the string index or to nothing because it will default to returning the rest of the string if you don't have a second parameter.
str.substr(1) will return bcdef
str.substr(1, 5) will also return bcdef

how to print char array in c++

how can i print a char array such i initialize and then concatenate to another char array? Please see code below
int main () {
char dest[1020];
char source[7]="baby";
cout <<"source: " <<source <<endl;
cout <<"return value: "<<strcat(dest, source) <<endl;
cout << "pointer pass: "<<dest <<endl;
return 0;
}
this is the output
source: baby
return value: v����baby
pointer pass: v����baby
basically i would like to see the output print
source: baby
return value: baby
pointer pass: baby
You haven't initialized dest
char dest[1020] = ""; //should fix it
You were just lucky that it so happened that the 6th (random) value in dest was 0. If it was the 1000th character, your return value would be much longer. If it were greater than 1024 then you'd get undefined behavior.
Strings as char arrays must be delimited with 0. Otherwise there's no telling where they end. You could alternatively say that the string ends at its zeroth character by explicitly setting it to 0;
char dest[1020];
dest[0] = 0;
Or you could initialize your whole array with 0's
char dest[1024] = {};
And since your question is tagged C++ I cannot but note that in C++ we use std::strings which save you from a lot of headache. Operator + can be used to concatenate two std::strings
Don't use char[]. If you write:
std::string dest;
std::string source( "baby" )
// ...
dest += source;
, you'll have no problems. (In fact, your problem is due to the fact
that strcat requires a '\0' terminated string as its first argument,
and you're giving it random data. Which is undefined behavior.)
your dest array isn't initialized. so strcat tries to append source to the end of dest wich is determined by a trailing '\0' character, but it's undefined where an uninitialized array might end... (if it does at all...)
so you end up printing more or less random characters until accidentially a '\0' character occurs...
Try this
#include <iostream>
using namespace std;
int main()
{
char dest[1020];
memset (dest, 0, sizeof(dest));
char source[7] = "baby";
cout << "Source: " << source << endl;
cout << "return value: " << strcat_s(dest, source) << endl;
cout << "pointer pass: " << dest << endl;
getchar();
return 0;
}
Did using VS 2010 Express.
clear memory using memset as soon as you declare dest, it's more secure. Also if you are using VC++, use strcat_s() instead of strcat().