KayacanV's blog

By KayacanV, 10 years ago, In English

how can i read to c++ string from position one not zero

  • Vote: I like it
  • +3
  • Vote: I do not like it

»
10 years ago, # |
  Vote: I like it 0 Vote: I do not like it

If you are using char*, you can make sth like

char s[1000];
gets(s + 1);
or
scanf("%s", s + 1);
  • »
    »
    10 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    no i am using std :: string

    • »
      »
      »
      10 years ago, # ^ |
        Vote: I like it +40 Vote: I do not like it

      s = "#" + s;

      • »
        »
        »
        »
        10 years ago, # ^ |
        Rev. 2   Vote: I like it +5 Vote: I do not like it

         I forgot, ty

        • »
          »
          »
          »
          »
          10 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          I actually use this whenever I use strings (though for not that long time). Got a problem xd? It is comfortable, makes everything much easier for those indexing from 1 (I belong to that set) and in fact it is linear in time, but time it executes is really neglectable, surely much shorter than that needed to read it.

        • »
          »
          »
          »
          »
          10 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          char * const str = &s[0] - 1;

      • »
        »
        »
        »
        10 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Is this constant time?

        • »
          »
          »
          »
          »
          10 years ago, # ^ |
            Vote: I like it +11 Vote: I do not like it

          I'm not sure, but it should be at worst O(n), and since you read the string, the program must be at least O(n), so it shouldn't matter anyway.

»
10 years ago, # |
  Vote: I like it -20 Vote: I do not like it
    char temp[1000];
    gets(temp + 1);
    temp[0] = ' ';  // Some char that should stay in 0's position in your string
    string s(temp)