2

I am trying to learn some of this beautiful language but I've got stuck on this. Problem is: Why does the last count shows only Witaj PJC not Witaj Cpp PJC? As you see function app has to append transformed 2nd word to 1st one. Thanks for any help.

If you could give me any good tutorial about pointers I would appreciate that. Thanks!

#include <iostream>
#include <string.h>
using namespace std;

void app(char *str2, char *str1){


    for(int i =0; i < strlen(str2); i++){
        *(str2++);
    }

    for(int i =0; i < strlen(str1); i++){
        *(str1++);
    }
    for(int i =0; i < strlen(str1); i++){

        *(str2)=*(str1);
        *(str2)++;
        *(str1)--;
    }
}

int main()
{
 char *str1 = "ppC ";

 char str2[20] = "Witaj";

 cout << str2 << endl;    // Witaj

 app(str2, str1);

 cout << str2 << endl;    // Witaj Cpp shows WitCpp

 app(str2, "CJP ");

 cout << str2 << endl;    // Witaj Cpp PJC shows WitPJ
    return 0;

}
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
jawjaw
  • 147
  • 1
  • 11
  • 1
    Ah... sizeof is not actual size of this string yes?.. Thanks – jawjaw Nov 13 '15 at 12:58
  • You should consider getting [a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), online tutorials are full of beginners' (and not-so-beginners') misconceptions. – molbdnilo Nov 13 '15 at 12:59
  • @zenith: I'd add that since `strlen` is `O(N)`, it should be called once *before* entering the loop to capture the return value once instead of calling it once per comparison by the loop, which would make it worse. – code_dredd Nov 13 '15 at 13:02
  • @jawjaw: `sizeof` does not give you the *length* of the string. Look at an easy-to-find C or [C++ reference](http://en.cppreference.com/w/cpp/language/sizeof) – code_dredd Nov 13 '15 at 13:04
  • ok i already knew it but forgot about this... Could you look at the corrected code? still something is not working right – jawjaw Nov 13 '15 at 13:05
  • `*(str2++);` What is the dereferencing (`*`) for ? – Gabriel Nov 13 '15 at 13:54

2 Answers2

4

Your problem is this sort of loops:

for(int i =0; i < strlen(str2); i++){
    *(str2++);
}

You can't move your pointer with str2++ and expect that strlen(str2) still returning the lenght of the original one.

For loop variables, in each iteration:

                 i  str2  strlen(str2)   condition
Iteration 1      0  Witaj     5            0 < 5  Ok
Iteration 2      1  itaj      4            1 < 4  Ok
Iteration 3      2  taj       3            2 < 3  Ok
Iteration 4      3  aj        2            3 < 2  Exit at 3rd character!!

Thus.. you only "move" your pointer 3 bytes.

Change your app function for that one:

void app(char *str2, char *str1){

   int nstr2 = strlen(str2);
   int nstr1 = strlen(str1);


    for(int i =0; i < nstr2; i++){
        *(str2++);
    }

    for(int i =0; i < nstr1; i++){
        *(str1++);
    }

    for(int i =0; i < nstr1; i++){

        *(str2++)=*(--str1);
    }

}

Anyway... this program is only for academic porpouses or you are thinking use it professionally?

David Isla
  • 619
  • 7
  • 19
  • Also, the deref is not needed, thus should be `str2++` etc. not `*(str2++)` Another comment, it might be a good idea to pass pointers like those above as const, so that you don't manipulate the original string. I would advise to pass in the two strings as const pointers, then from the append method return a newly allocated string `char*` with the appended one. Then you can work on temp pointers inside the function and also run no risk of fiddling with the original data beween calls. – Richard Tyregrim Nov 13 '15 at 14:11
0

And for some functioning code for just string appending, i scribbled this...

Note that you should make a const call instead, and if you want to reverse one of the strings (a bit unclear from your question) it should be done prior to appending.

Example of string append (rather unsafely and rudimentary) using a new allocation:

char* app(char *str2, char *str1){

  char* appendedstring = (char*)malloc(sizeof(char)*20);
  char *temp = str1;
  char *temp2 = str2;
  int stringlen1 = strlen(str1);
  int stringlen2 = strlen(str2);

//Copy string 1
for (int i = 0; i < stringlen2; i++){
    appendedstring[i] = *temp2;
    temp2++;
}

//Append string 2
for (int i = 0; i < stringlen1 + 1; i++){

    appendedstring[stringlen2 + i] = *temp; 
    temp++;
}

  return appendedstring;
}

int main()
{
  int t;
  char *str1 = "ppC ";

  char str2[20] = "Witaj";

  cout << str1 << endl;

  cout << str2 << endl;    // Witaj

  char* newstr = app(str2, str1);

  cout << newstr << endl;    // Witaj Cpp shows WitCpp

  char* newstr2 = app(str2, "CJP ");

  cout << newstr2 << endl;    // Witaj Cpp PJC shows WitPJ


  return 0;

}
Richard Tyregrim
  • 582
  • 2
  • 12