how can I use string variable in system("say string variable")? - c++

/* I thought of doing in this way, but it invalid.
so any help will be appreciated. */
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name = "Karma";
string greet = "How was your day?";
string sums = name + greet;
system("say %s", sums) // not working
// system("say sums") not working
return 0;
}

You can use:
system(("say" + sums).c_str())
Instead of:
system("say %s", sums)

Related

How to extract substring by using start and end delimiters in C++

I have a string from command line input, like this:
string input = cmd_line.get_arg("-i"); // Filepath for offline mode
This looks like a file as below:
input = "../../dataPosition180.csv"
I want to extract out the 180 and store as an int.
In python, I would just do:
data = int(input.split('Position')[-1].split('.csv')[0])
How do I replicate this in C++?
Here's a (somewhat verbose) solution:
#include <string>
#include <iostream>
using namespace std;
int main() {
string input = "../../dataPosition180.csv";
// We add 8 because we want the position after "Position", which has length 8.
int start = input.rfind("Position") + 8;
int end = input.find(".csv");
int length = end - start;
int part = atoi(input.substr(start, length).c_str());
cout << part << endl;
return 0;
}
#include <string>
#include <regex>
using namespace std;
int getDataPositionId (const string& input){
regex mask ("dataPosition(\\d+).csv");
smatch match;
if (! regex_search(input, match, mask)){
throw runtime_error("invalid input");
}
return std::stoi(match[1].str());
}

How to change the order of an array in c++

I am try to change the order of an array of char like this :
char arr_char[]="ABCDEFGHIJABCDEFGHIJ";
i used the rand() function in the following code :
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char arr_char[]="ABCDEFGHIJABCDEFGHIJ";
int arrSize=sizeof(arr_char)-1;
srand(time(0));
for(int i=0;i<20;i++)
{
cout<<arr_char[rand() % arrSize]<<" ";
}
}
but the rand function repeat some characters more than twice and i want to change the order of the array in which every characters repeat only twice not more .
This will probably suffice
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string arr_char = "ABCDEFGHIJABCDEFGHIJ";
random_shuffle(arr_char.begin(), arr_char.end());
cout << arr_char;
}

use std::vector<char> in C++/cLi [duplicate]

I have a function in C++ that have a value in std::string type and would like to convert it to String^.
void(String ^outValue)
{
std::string str("Hello World");
outValue = str;
}
From MSDN:
#include <string>
#include <iostream>
using namespace System;
using namespace std;
int main() {
string str = "test";
String^ newSystemString = gcnew String(str.c_str());
}
http://msdn.microsoft.com/en-us/library/ms235219.aspx
Googling reveals marshal_as (untested):
// marshal_as_test.cpp
// compile with: /clr
#include <stdlib.h>
#include <string>
#include <msclr\marshal_cppstd.h>
using namespace System;
using namespace msclr::interop;
int main() {
std::string message = "Test String to Marshal";
String^ result;
result = marshal_as<String^>( message );
return 0;
}
Also see Overview of Marshaling.
As far as I got it, at least the marshal_as approach (not sure about gcnew String) will lead to non ASCII UTF-8 characters in the std::string to be broken.
Based on what I've found on https://bytes.com/topic/c-sharp/answers/725734-utf-8-std-string-system-string I've build this solution which seems to work for me at least with German diacritics:
System::String^ StdStringToUTF16(std::string s)
{
cli::array<System::Byte>^ a = gcnew cli::array<System::Byte>(s.length());
int i = s.length();
while (i-- > 0)
{
a[i] = s[i];
}
return System::Text::Encoding::UTF8->GetString(a);
}

Segmentation fault in reversing string program

I am trying to reverse a string. Can someone explain me why this is giving me segmentation fault?
#include <iostream>
#include <string>
using namespace std;
int main(){
string str,rstr;
int len=str.length(),i=0;
cin>>str;
while(str[i]!='\0'){
rstr[--len]=str[i++];
}
rstr[str.length()]='\0';
cout<<rstr;
return 0;
}
P.S.: Need to reverse it without using library functions.
If you want to go the way you are doing it, for practice purposes, try this changes and start from there
#include <iostream>
#include <string>
using namespace std;
int main(){
string str,rstr;
cin>>str; // --- Moved this line up
rstr = str; // --- Added this line
int len=str.length(),i=0;
while(str[i]!='\0'){
rstr[--len]=str[i++];
}
rstr[str.length()]='\0';
cout<<rstr;
return 0;
}
Or just use reverse iterator
std::string s = "Hello";
std::string r(s.rbegin(), s.rend());
str is nothing but a declared string here:
int len=str.length(),i=0;
So you can't do str.length()
Do something like:
#include <iostream>
#include <string>
using namespace std;
int main(){
string str,rstr;
int len,i=0;
cin>>str;
len = str.length();
while(str[i]!='\0'){
rstr[i++]=str[--len];
}
rstr[str.length()]='\0';
cout<<rstr;
return 0;
}

How to compare char variables (c-strings)?

#include <iostream>
using namespace std;
int main() {
char word[10]="php";
char word1[10]="php";
if(word==word1){
cout<<"word = word1"<<endl;
}
return 0;
}
I don't know how to compare two char strings to check they are equal. My current code is not working.
Use strcmp.
#include <cstring>
// ...
if(std::strcmp(word, wordl) == 0) {
// ...
}
Use std::string objects instead:
#include <iostream>
#include <string>
using namespace std;
int main() {
string word="php";
string word1="php";
if(word==word1){
cout<<"word = word1"<<endl;
}
return 0;
}
To justify c++ tag you'd probably want to declare word and word1 as std::string. To compare them as is you need
if(!strcmp(word,word1)) {
word and word1 in your submitted code are pointers. So when you code:
word==word1
you are comparing two memory addresses (which isn't what you want), not the c-strings they point to.
#include <iostream>
**#include <string>** //You need this lib too
using namespace std;
int main()
{
char word[10]="php";
char word1[10]="php";
**if(strcmp(word,word1)==0)** *//if you want to validate if they are the same string*
cout<<"word = word1"<<endl;
*//or*
**if(strcmp(word,word1)!=0)** *//if you want to validate if they're different*
cout<<"word != word1"<<endl;
return 0;``
}