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 6 years ago.
Improve this question
word[0] returns the entire string instead of the first element.
How do I access elements in a const string& word that I obtain from a function.
void something(const string& word)
{
int i = 0;
for (int i = 0; i < word.size(); i++)
cout << word[i];
}
Is there a way to convert it into a string??
Please help!!
Works correctly in my system, no problem. Here's sample code:
#include <iostream>
#include <string>
void myfunc(const std::string& word){
std::cout << word[0] << std::endl;
}
int main(void){
myfunc("test");
}
Output:
~$ ./a.out
t
If you just want to convert from const string into string, just copy it.
std::string new_word = word;
std::cout << new_word;
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 12 months ago.
Improve this question
#include <bits/stdc++.h>
using namespace std;
int main()
{
char *str;
gets(str);
int size = strlen(*(&str));
//How to iterate through this str which is acting like a string here
return 0;
}
//I'm trying to print each char in a new line.
Ignoring all the other problems, such as using an uninitialized pointer, using gets (it's so bad it's been removed from C and C++), including bits/stdc++.h, not using std::string and std::getline...
Using your size variable, you can use loop like this:
for(int index = 0 ; index < size ; ++index) {
std::cout << "character at index " << index << " is '" << str[index] << "'\n";
}
But note that your code will crash at gets and never get to this loop. Please find better learning material to get started with C++!
PS. To get your code to not crash, change char *str; to char str[10000];... Then that program should run and you are unlikely to accidentally cause a buffer overflow. Still, I repeat, get better learning material!
The character pointer str doesn't point to any char object and has not been initialized.
Second, the function gets has been deprecated in C++11 and removed in C++14.
A better way would be to use std::string instead as shown below:
#include <string>
#include <iostream>
int main()
{
std::string str;
//take input from user
std::getline(std::cin, str);
//print out the size of the input string
std::cout << str.size() << std::endl;
//iterate through the input string
for(char& element: str)
{
std::cout<<element<<std::endl;
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to find index of '-' in string "book-Buch". What should I do? I mustn't use this function or any others. Any ideas?
int indexOf(int ch, int fromIndex)
#include <iostream>
#include <string>
int main() {
std::string word("book-Buch");
// The easy way
std::cout << word.find("-", 0) << '\n';
// The manual way
for (std::size_t i = 0; i < word.length(); ++i) {
if (word[i] == '-') {
std::cout << i << '\n';
}
}
}
If you just want to find the index that a certain character occurs in, you just need to look at each character and check if it's the one you want.
A string can be treated as an array of characters. It's unknown whether you actually want an array of string objects, or are just confused.
Other questions that would need to be answered: do you need to find all occurrences, or just the first? Are you reading the words out of a file? You don't clearly explain how a file comes into play.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to extract 3 numbers out of the semantic version string which has the following format: "v%d.%d.%d"
Here's my example code:
std::string myVersion = "v3.49.1";
int versionMajor, versionMinor, versionPatch;
getVersionInfo(myVersion, versionMajor, versionMinor, versionPatch);
std::cout << versionMajor << " " << versionMinor << " " << versionPatch << '\n';
The expected result:
3 49 1
How can I design the function getVersionInfo()?
What would be the most elegant solution?
Use sscanf:
void getVersionInfo(std::string version, int &major, int &minor, int &patch) {
sscanf(version.c_str(), "v%d.%d.%d", &major, &minor, &patch);
}
Try it here
Maybe You can try this
#include <iostream>
using namespace std;
int main() {
string s="v3.49.1";
int major,minor,patch;
int x=s.find(".");
//We dont want v symbol so start from 1 till 1st dot
major=stoi(s.substr(1,x));
cout<<major;
//Then find second dot from first symbol and copy as sub string
int y=s.find(".",x+1);
minor=stoi(s.substr(x+1,y-x));
cout<<minor;
//After that remaining string will be patch version
patch=stoi(s.substr(y+1));
cout<<patch;
return 0;
}
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 5 years ago.
Improve this question
please find below program, the sort output is not proper.
string pru[] = { "ruthvi$p", "uthvi$pr", "thvi$pru", "hvi$prut", "vi$pruth", "i$pruthv", "$pruthvi", "pruthvi$" };
sort(pru, pru + 8, cmp);
for(int i = 0; i < 8; i++)
cout << pru[i] << " ";
output is
$pruthvi hvi$prut i$pruthv pruthvi$ thvi$pru ruthvi$p uthvi$pr vi$pruth
mistake "thvi$pru" is before "ruthvi$p"
It is not clear how cmp is defined but you can use this code and you will get the expected result.:)
#include <string>
#include <iterator>
#include <algorithm>
//...
std::string pru[] =
{
"ruthvi$p",
"uthvi$pr",
"thvi$pru",
"hvi$prut",
"vi$pruth",
"i$pruthv",
"$pruthvi",
"pruthvi$"
};
std::sort(std::begin(pru), std::end(pru));
for (const auto &s : pru) std::cout << s << std::endl;
The output is
$pruthvi
hvi$prut
i$pruthv
pruthvi$
ruthvi$p
thvi$pru
uthvi$pr
vi$pruth
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 5 years ago.
Improve this question
I know how to use a string to calculate the number of characters, but I'm not sure how to use a function to do that. have to use CSTRING. THANK YOU ALL
#include <cstring>
char a[10];
cout << "Please enter anything: ";
cin.getline(a,10);
cout << "You type " << strlen(a) << " letters long"<<endl;
You're probably looking for std::string since you're question mentioned C++ and not only C.
include <string>
std::string myString = "Something";
size_t stringLength = myString.size();
It's simple. Just type your code inside a function()
int stringlengthfunction()
{
char str[80];
int i;
cout<<"\n enter string:";
cin.getline(str,80);
int n=strlen(str);
cout<<"\n lenght is:"<<n;
getch();
return 0;
}
or pass your string as a parameter to the function
int stringlengthfunction(string str)