Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - Specify maximum character for std::cout when printing char*

Tags:

c++

std

c++17

Let's say I have a const char* that I want to print with std::cout but I only want the first X characters to be printed. Is there a way to tell that to std::cout? (Without inserting a terminating-null into the string or making a temporary copy.)

like image 795
AdyAdy Avatar asked Aug 03 '18 13:08

AdyAdy


People also ask

How do I print the same character multiple times in C++?

You have to write a loop or initialize a std::string with a/2 '*' characters to get that working. Your logic to count number of stars that should get printed is correct.

How do I print a character in cout?

Then, how to print character? We can use cast type here, by casting into char we are able to get result in character format. We can use cout<<char(65) or cout<<char(var), that will print 'A'. (65 is the ASCII value of 'A').

What does printf actually do?

The printf function (the name comes from “print formatted”) prints a string on the screen using a “format string” that includes the instructions to mix several strings and produce the final string to be printed on the screen.

How to print the maximum number of characters in a string?

The task is to print the maximum occurring character in the input string. If 2 or more characters appear the same number of times, print the lexicographically (alphabetically) lowest (first) character. Examples: ‘t’, ‘e’ and ‘s’ appears 2 times, but ‘e’ is the lexicographically smallest character.

How to print more than one variable with Cout in C++?

Note: More than one variable can be printed using the insertion operator (<<) with cout. Below is the C++ program to implement the above approach: The cout statement can also be used with some member functions:

How many characters can a character variable store in C?

A character variable can store only a single character. In the example above, we have declared a character type variable named ch. We then assigned the character h to it. Note: In C and C++, a character should be inside single quotation marks. If we use, double quotation marks, it's a string.

What is the ASCII value of Char Char Char?

Char values are interpreted as ASCII characters. ASCII is an acronym for American Standard Code for Information Interchange. It states a specific way of representing English characters in the form of numbers. To see the ASCII value of a character, we pass it to the int () function.


1 Answers

C++ 17 introduces string_view

#include <string_view>
#include <iostream> 

char message[] = { "my long char message" };
int length = some_number; 

int main() { 
      string_view str(message); 
      std::cout << str.substr(0, length) << std::endl; 
}

I have not tried to compile the above code. string_view is basically a string except that it does not 'own' its contents (will not delete the internal pointer after use).

like image 195
Joseph Franciscus Avatar answered Nov 04 '22 05:11

Joseph Franciscus