Why does this code only run like 15 letters?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kataria1231
    New Member
    • Nov 2020
    • 1

    Why does this code only run like 15 letters?

    #include <iostream>
    #include <string>

    using namespace std;


    int main()
    {

    string setning;
    string temp;

    getline(cin,set ning);

    for (int i = 0 ; i < setning.size(); i++ ){
    temp[0+i] = setning[setning.size()-1-i];
    cout<<temp[i]<<' ';
    }


    return 0;
    }

    I figured it out
    Last edited by kataria1231; Nov 26 '20, 04:34 PM. Reason: Couldn't find a way to delete this question.
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 655

    #2
    Code:
    temp[0+i] = setning[setning.size()-1-i];
    0 has no significance here.

    Comment

    • AjayGohil
      New Member
      • Apr 2019
      • 83

      #3
      Hi,

      Your code is perfectly working for any number of letter and it reverse string.
      you can try below method for reverse string:

      Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.

      Comment

      Working...