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.
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 3 years ago.
Improve this question
I am trying to randomly display a user input string using c++ but I couldn't find a way to do.
Currently I am pre defining some strings
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
srand(time(0));
const string wordlist[4] = {"hi","hello","what's up","wassup"};
string word = wordlist [rand()%4];
cout<<word;
return 0;
}
What I want is:- I don't want to pre define them. I want the user to type in 4 words and I will display a word from the 4 words given by the user (randomly).
To do that you will have to first remove const qualifier from wordlist array.
srand(time(0));
std::vector<string> wordlist(4);
for(auto& s: wordlist) std::cin>>s;
string word = wordlist [rand()%4];
cout<<word;
Line 3 is C++11's range based for loop, this way I can easily loop over elements of std::vector<string> without indices.
If there are multiple words in a string then use getline(cin,s) accordingly. Then input each string in a new line. But be careful when mixing cin and getline for taking input.
You can use std::array if the size is going to be fixed (i.e. 4) as mentioned in comments.
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 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').
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);