c++ int store as character in char* conversion without any API - c++

how can we store int value in char* as representing character in c++.. for example, i want to store 10..char* p is a character pointer and i want to store 10 as character in that pointer...because i want to write iteration that generates character stream based on integer value.how to do char concatenation with integer(as char) with The similar java code be as:
for(int i=0; i<10; i++)
{
string temp=value+i;//here i want to use char* to represent string in c++
System.out.println(temp);
}

I know you said C++, but you also said char* so i am going to treat it as C. With C, you can't really do concatenation like that. The best way to do it would be to calculate the number of characters required, malloc that, then just store the characters in the char array. And remember to free it when you're done using it! In C, you have to do everything yourself!

I'm a little confused about what you're trying to do, but here's some information that I think will probably help you work it out:
In C++, you should primarily use std::string to hold strings of characters.
In regular C, the convention is to use a char* to hold a list of characters - these char*'s have to be null terminated ending in \0 so that your code knows where to stop printing the string of characters.
Preferring the C++ way, you can concatenate strings with the + operator:
Here's an example:
std::string myString = "H";
myString += "e";
myString += "l";
std::cerr << myString; //prints "Hel" to console.
You can also use a string stream which can mix data types:
std::stringstream ss;
ss << "h" << "e" << 7 << myCustomType << std::endl;
One other thing that's good to know is you can store an integer value in a char and it will work out the ascii representation when you print it.
For example:
char x = 65; //x = capital A.

Related

Parse int from a string without regex?

If I have a string where there is some word and some number (like "Text 125"), is it possible to get this number and convert it to int without using regex?
Yes, you could use a stringstream if you know that it's a word followed by a number.
std::stringstream ss("Text 125");
std::string buffer; //a buffer to read "Text" into
int n; //to store the number in
ss >> buffer >> n; //reads "Text" into buffer and puts the number in n
std::cout << n << "\n";
Edit: I found a way to do it without needing to declare a pointless variable. It's a bit less robust though. This version assumes that there is nothing after the number. std::stoi will work regardless of the number of spaces between the word and the number.
std::string str("Text 125");
int n = std::stoi(str.substr(str.find_first_of(' ') + 1));
std::cout << n << std::endl;
If you know exactly the format of the expected string, i.e. that it
always starts with "Text"
followed by a space
followed by a number
and then ends
(so in regex terms, it's /^Text (\d+)$/)
you can combine std::find and std::substr.
const std::string inputStr = "Text 125";
const std::string textStr = "Text ";
const std::size_t textPos = inputStr.find(textStr);
const std::size_t numberPos = textPos + textStr.length();
const std::string numberStr = inputStr.substr(numberPos);
const int numberInt = std::atoi(numberStr.c_str());
However, that only works in these specific circumstances. And even if /^Text (\d+)$/ is the only expected format, other input strings might still be possible, so you'll need to add the appropriate length checks, and then either throw an exception or return an invalid number or whatever you need to happen for invalid input strings.
#DanielGiger's answer is more generally applicable. (It only requires the number to be the second string, covering the more general case of /^\S+\s+(\d+)/)

How to explain weird characters at the end of my char array, and the wrong result of using strlen function in my code

I am a complete beginner so the code may seem to be easy, but I cannot find a solution why it returns such values as:
input: kkkk
output:
14
kkkkřřřř╩┬ëŢ
Suprisingly the code works fine with online compilators, but not with the Visual Studio.
#include<iostream>
#include<string.h>
int main()
{
char word[20];
std::cin >> word;
int length = strlen(word);
int p = length - 1, i = 0;
char *var=new char [length];
while (i < length&&p>=0)
{
var[i]= word[p];
p--;
i++;
}
std::cout <<strlen(var)<<endl<< var;
if (!strcmp(var, word)) std::cout << "\nThe word is a palindrome";
return 0;
}
I can not use strings because my University doesn't allow to do so. I also know there are many different ways to attend to this problem but I just really want know what I have done wrong in this one :/
Your "copy routine" copies each character, but it does not copy the string termination character. Note that C-style strings as used in functions like strlen or strcmp need to be 0-terminated, and even cout <<, when getting a parameter of type char*, treats this as a C-style string: It will read until finding the terminating '\0', and if you do not write one, it will read beyond the boundaries you think it should do.
If your write
...
}
var[length] = '\0';
std::cout <<strlen(var)<<endl;
...
it should work.

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.

Counting characters in a character pointer

I'm brushing up on some C++ and so one of the problems I'm trying to solve is counting characters from a character pointer and check it against what I expect to see. However in my solution, I noticed a peculiar result. I passed in a reference to a char to my function and it returned a count of 3. Why would the reference test return back a count of 3 for a reference to a character?
I realize the character doesn't have a null terminator and so the code keeps counting but it does eventually return a result, so that means the solution falls short. Any ideas to make it more robust? Here is my solution and result.
CountCharacters.cpp
#include <cstdio>
#include <iostream>
#define ASSERT_EQUALS(paramx1, paramx2) \
{\
int param1 = paramx1;\
int param2 = paramx2;\
if (param1==param2)\
std::cout << "PASS! param1=" << param1 << " param2=" << param2 << std::endl;\
else\
std::cout << "FAIL! param1=" << param1 << " param2=" << param2 << std::endl;\
}
int countCharacters(const char * characters);
int main()
{
char character = '1';
ASSERT_EQUALS(countCharacters("string8\0"), 7);
ASSERT_EQUALS(countCharacters("\0"), 0);
ASSERT_EQUALS(countCharacters(""), 0);
ASSERT_EQUALS(countCharacters(NULL), 0);
ASSERT_EQUALS(countCharacters(&character), 1);
ASSERT_EQUALS(countCharacters('\0'), 0);
return 0;
}
int countCharacters(const char * characters)
{
if (!characters) return 0;
int count = 0;
const char * mySpot = characters;
while (*(mySpot) != '\0')
{
std::cout << "Count=" << count << " mySpot=" << *(mySpot) << std::endl;
count++;
mySpot++;
}
return count;
}
Results:
PASS! param1=7 param2=7
PASS! param1=0 param2=0
PASS! param1=0 param2=0
PASS! param1=0 param2=0
FAIL! param1=2 param2=1
PASS! param1=0 param2=0
You're not passing a reference to a character. You're passing a pointer. Specifically, this is a pointer:
&character
The & and * symbols are a bit confusing when learning c++. Depending on where their located, they can be a pointer or a reference:
char character = '1'; // <- char variable
char* characterPtr = &character; // <- pointer to the char variable
char& characterRef = *characterPtr; // <- reference to the char variable
So, you're passing a pointer to a character and your function is treating it like the head of a string and counting characters until it hits a nullptr. There just happened to be one a few chars away, which is why you're getting the value 2.
EDIT: C/C++ has no native string type, just character types. So you need libraries like the ones you're including to treat characters like heads of strings. The convention is that a nullptr terminates the string. So, you're exercising that convention nicely, but also demonstrating the issue that there's no difference between the pointer to a character and the pointer to the character at the head of a string, so its easy to accidentally pass a pointer to a character to a function that's expecting a string. Things get really interesting if the function starts copying characters into that 'string' because it assumes you allocated that memory, but it could be other data that then gets squashed.
Aside from being dangerous, the other major downside of using character strings is they're tedious to manipulate them, since there's no native functions. So, nice libraries like STL have been written to solve these problems. They don't require pointers, so are a lot safer to use (you can use references instead and do bounds checking), and they have a lot of built in methods, so cut down on the amount of coding you need to do.

How do I combine two strings?

The question requires combining two strings(the longer string in the front and the shorter one after the longer one) without using <string> header file.Each string inputted can't exceed 20 characters.
My logic behind this is:
first use strlen to get the length of the str1 and str2,
use str3 to store the longer string, and str4 to store the shorter.
add str3 and str4 to str5
Here is my code:
#include<iostream>
using namespace std;
int main()
{
// combine two strings , longer in the front, and shorter
// after it. do not use strcat
char str1[20],str2[20],str3[20],str4[20],str5[40];
// str1 and str2 stores original data, str3 stores longer
// and str4 stores shorter, str5 stores total
int j=0;
cin.getline(str1,20);
cin.getline(str2,20);
if(strlen(str1)<=strlen(str2))
// give longer string value to str3,shorter to str2
{
for (int i=0;i<20;i++)
{
str3[i]=str2[i];
str4[i]=str1[i];
}
}
else
{
for (int i=0;i<20;i++)
{
str3[i]=str1[i];
str4[i]=str2[i];
}
}
for(j=0;str3[j]!='\0';j++)
{
str5[j]=str3[j];
}
for(int i=j;i<40;i++)
for(int m=0;m<20;m++)
{
str5[i]=str4[m];
}
cout<<str5<<endl;
return 0;
}
Here is the ouput:
What's my problem here? What are those characters in between the two strings? Thank you!!
Especially since you explicitly mentioned being a beginner, the solution is to use std::string:
#include <iostream>
#include <string>
int main() {
std::string a;
getline(std::cin, a);
std::string b;
getline(std::cin, b);
// Ensure that the longer string goes to the front.
if (a.size() < b.size());
swap(a, b);
std::string result = a + b;
std::cout << result << '\n';
// Or, simply:
std::cout << a << b << '\n';
}
The message here is that C++, despite its quirks, is a very high level language if you rely on its library instead of implementing every low level operation from scratch.
Everything is fine (!) up to this point
for(int i=j;i<40;i++)
for(int m=0;m<20;m++) // This loop runs m=0 to 20 for each position of i
{
str5[i]=str4[m];
}
For each index i you are copying in all 20 elements from str4, leaving just the value at str4[19] which could be anything
Just increment i and m by one together
int m = 0;
for(int i=j;i<40;i++)
{
str5[i]=str4[m++];
}
You are copying the entire 20 characters, 40 characters in the loop into the variables. stop copying when you find a '\0' character.
But using the std::string will make life simpler :)
Using std::string is nice and all but here's a few tips for working with char*:
1) You shouldn't copy strings to separate shorter and longer string, just use pointers and then work with these pointers, something along these lines:
const char *longer_string = 0, *shorter_string = 0;
if(strlen(str1)<=strlen(str2))
{
shorter_string = str1;
longer_string = str2;
}
else
{
shorter_string = str2;
storter_string = str1;
}
2) Using strcpy and strcat to combine strings could make life a lot easier:
char *combined_string = new char [strlen (shorter_string) + strlen (longer_string) + 1];
strcpy (combined_string, longer_string);
strcat (combined_string, shorter_string);
Some compilers would say that these functions aren't safe and you have to stick to _s versions, but I guess it's entirely up to you.
Since this is obviously homework: I'll just point out the existence of the function strcat, and the fact that you can use char* to the arrays, and just swap them, without having to recopy anything between the initial read and the concatenation (which means that you only need two arrays: one for each of the inputs, and one for the final value).
And also, when calculating sizes, etc. do not forget that C style strings have an extra '\0' at the end, and make allowances for it.
As #David Sykes has pointed out, the problem is with your for loop. So when you read input from cin ,it is not necessary that your input string contains 20 character. But in you form loop you are looping through those string beyond their length which may contains garbage characters. Example
char str1[20]
cin.getline(str1,20);
cout << str1[19] << endl;
Suppose your input for above code is "ABCD" which contains only 4 characters but your array has capacity of 20. So the remaining space has junk characters and when you will try to print any thing beyond actual length you will get wild character as you are getting in your code.