Using strings in C++ -
i made program in java new in c++ , using strings has been imposible. wondering if point me in direction me "translate" program c++. how use strings , maybe see in code worth mentioning. note: not asking of direct translation, info me it, code reference.
import java.io.bufferedwriter; import java.io.outputstreamwriter; import java.util.scanner; public class dragoncurve{ public static void main(string[] args){ try{ scanner sc = new scanner(system.in); bufferedwriter log = new bufferedwriter(new outputstreamwriter(system.out)); int n; while((n = integer.parseint(sc.nextline()))!=-1){ log.write(dragon(n)+"\n"); } log.flush(); }catch(exception e){ e.printstacktrace(); } } public static string dragon(int n){ if(n>2){ string aux = dragon(n-1); return aux+"l"+reverse(aux); }else if(n==2) return "llr"; else if(n==1) return "l"; else if(n==0) return ""; return ""; } public static string reverse(string aux){ string ans = ""; for(int = aux.length();i>0;i--){ if(aux.charat(i-1)=='l') ans+='r'; else ans+='l'; } return ans; } }
to started, here how write i/o . (many books cover badly).
i left string handling section figure out. not dissimilar java, have bear in mind std::string
not include automatic functions converting integer or character string; can't use +
operator. learn that, read this thread.
i recommend learning book. if don't have book, can cppreference.com or other sites see how std::string
works.
note std::reverse
exists, don't have write own reverse
function.
#include <iostream> #include <string> #include <sstream> std::string dragon(int n) { // exercise reader. } int main() { std::string s; int n; while ( std::getline( std::cin, s ) ) { std::istringstream iss(s); while ( iss >> n ) std::cout << dragon(n) << std::endl; } }
Comments
Post a Comment