Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Trying to write the following function but confused, as get() only reads in the first character?
Write C-string's chars to the screen one char at a time.
void writeString(const char*)
Rule:
cannot use [].
Hints:use put();
make use of '\0' – but don't write it out.
It sounds like you just need a simple loop to output the string. Something like this perhaps.
void writeString(const char* str)
{
while(str++ != '\0') put(*str);
}
The while(str++ != '\0') will iterate over the string buffer pointed to by str and output each character. It also increments the str pointer to the next character and checks for null terminator ('\0').
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is it possible to substring console output of an std::string using std::string_view?
For example:
std::string toolong {"this is a string too long for me"};
std::string_view(toolong);
// do something...
expected console output: this is a string
Yes, it's called substring-ing.
std::string toolong {"this is a string too long for me"};
std::string_view view(toolong);
std::cout << view.substr(0, 16);
Alternatively, you can use the remove_prefix() and remove_suffix() methods as well.
Example:
view.remove_suffix(16); // view is now "this is a string"
view.remove_prefix(5); // view is now -> "is a string"
If you want to do it in-place without creating a variable of string_view, use substr()
std::string toolong {"this is a string too long for me"};
std::cout << std::string_view (toolong).substr(0, 16);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to turn the inputted character array into all lowercases. The reading stops at the '.' character. I have to do it this way, without using the string variable nor any function inside the library.
#include <iostream>
using namespace std;
int main(){
char s[200], *p=s;
cin.getline(s, 200, '.');
while(p){
if('a' <= *p && *p <= 'z') *p += 'A'-'a';
p++;
}
cout << s;
}
The algorithm is supposed to check every character in the array until it meets a null pointer. For every character, it should then check if it is a lowercase character. If it is, it changes it into an uppercase letter (by decreasing 'a' it memorises the difference between the first letter of the alphabet and the letter it is referring to, by increasing 'A' it adds that difference to the first letter of the uppercase alphabet, so to speak, thus obtaining the uppercase version of the letter).
I've no clue where my mistake is, but my compiler crashes when I try to run it.
while (p)
should be
while (*p)
If you're looking to stop on the null terminator, you need to dereference the pointer or you'll test that the pointer isn't null instead.
Sidenote: You may find the std::tolower function helpful.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have been looking and I have not found anything on scanf that really helps me. I am completly new to this and was hoping for help on reading a string with scanf. The first thre numbers can be any digits. I was attempting to read them into a variable int. the last one in the string is a char. this is my string
(1,2,123, 0)
(1,2,1,s)
This is my code:
int i,j,k;
char c, final;
scanf ("%c", c, "%d",&i, "%c", c, "%d", &j, "%d",&k, "%c", final);
I know this is not right but any help is appreciated
If the first 3 are digits and the last one is a character and are seperated by a space,which you want to assign to 3 integer variables and a character variable, use scanf like this:
int a,b,c;
char ch;
scanf ("%d%d%d %c",&a,&b,&c,&ch);
Or else if you want to extract 3 integers and a character from a string , use sscanf. It is not possible to do it with scanf.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
recently I made a program that, given a string, the function returns its corresponding ascii code. For example, the string "h", the function returns an int 104. Now, I want to do the reverse process, ie, given an int, return its corresponding ascii character. For example, given the 104 int return the string "h". Please, help.
Looking at the string constructors, we can see one that takes a count and a character value. So we can use that:
return std::string(1, ascii_value);
You probably don't need a whole string for this. Given that you're looking for a single character, the correct type to use is a char:
int x = 65;
char xc = (char)x;
assert(xc == 'A');
char c;
...
std::string mystring(1, c);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is for some added functionality that I need to build into an existing library. I am trying to to check if a string contains a substring. I have pointers to the start and one-past-the-end of the main string,(which is a substring of a much larger string) and the word to search for is a String datatype.
char * start;
char * end;
String wordtosearchfor
I read about find in C++, but I am having trouble figuring out how to work with the pointer inputs. Is there an inbuilt function that works with inputs in this form? Or would it be efficient to read from start to end into a new String that can be used with find?
Thanks!
First of all you can indeed use member function find of class std::string. For example
char * start;
char * end;
std::string wordtosearch;
// setting values for variables
std::string::size_type n = wordtosearch.find( start, 0, end - start );
Also there is standard algorithm std::search declared in header <algorithm>
For example
char * start;
char * end;
std::string wordtosearch;
// setting values for variables
std::string::iterator it = std::search( wordtosearch.begin(), wordtosearch.end(),
start, end );
In these examples I suppose that end points after the last character of seached substring.