Can anyone explain in simple terms how I can traverse a string to manipulate it element-by-element using pointers in C++??
I don't really know the way it works in C++, because in C++ the 'default' std::string is a class (or maybe a template, I don't know ) in C, It's accomplished using pointer arithmetic: http://ideone.com/DKZRh
Ok suppose i have a string and i want to replace all vowels by '#' char str[5]='abcde'; char *str1=str; for(i=0;i<5;i++) { if(*(str1+i)=='a') // give all conditions *(str1+i)='#'; }
http://www.cplusplus.com/reference/string/string/find/ http://www.cplusplus.com/reference/string/string/replace/ and iterating through the vowels should do it.
Well, if you are programming in C++, use std::string and don't use pointers into it if you don't have to. A simple example: ... string my_string = "Fred Derf"; for (int x = 0; x < my_string.length(); x++) cout << "I found the letter " << my_string[x] << endl; ... If you are asking this to learn about pointers, then the code might look like this ... const char* my_string = "Fred Derf"; for (const char* c = my_string; *c ; c++) cout << "I found the letter " << *c << endl; ... The second example walks the array of characters until the trailing NULL is found, thus causing the *c expression to be interpreted as "false"
Join our real-time social learning platform and learn together with your friends!