0

So I make an array of string pointers and put a string in at position 0 of the array. If I don't know the length of the string in word[0] how do I find it? How do I then manage that string, because I want to remove the "_." and "." part of the string so I will be left with "apple Tree".How do I resize that string? (functions like strcpy,strlen, or string.end() didn't work, I get errors like "can't convert string to char*" etc)

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <string>

using namespace std;

int main()
{
    int counter=0;
    string* word = new string[0];

    word[0] = "apple_.Tree.";

    return 0;
}

Edit:what i want to do is make a dynamic array of strings(not using vector) and then edit the strings inside

Jesepy
  • 37
  • 6
  • 7
    _`string* word = new string[0];`_ You're allocating an array of 0 strings. Are you sure this is what you want to do? – mediocrevegetable1 Jun 04 '21 at 12:21
  • 1
    My advice is to get rid of the `new`. You almost never have to dynamically allocate a `std::string`. I don't recall doing this in the last 20 years of professional c++ development. – drescherjm Jun 04 '21 at 12:24
  • 2
    If I squint, I "suspect" you are taking `std::string` being C++'s equivalent of C's `char *` way too literally. –  Jun 04 '21 at 12:24
  • I'0m not sure what a _dynamic array_ is supposed to be, but I suspect that `std::vector` does exactly what you want. – Jabberwocky Jun 04 '21 at 13:44
  • Dynamic like i add 10 strings into an array of 10 and i have 10 more strings(20 total) to add to the array but the array is full,so i use new and give 10 more space for the other strings – Jesepy Jun 04 '21 at 13:55
  • 1
    If you are forced to not use a `std::vector` because of an academic requirement using `new` may be your only way but you need to change the size as allocating 0 strings is not helpful. It won't expand after allocation and you can't even use `word[0]` as that would have required you to allocate at least 1 string. – drescherjm Jun 04 '21 at 13:56
  • @Jesepy you have actually two different questions: one dealing with the dynamic array, and one dealing with the removal of `_` and `.` from a `std::string`. You should ask one question at a time – Jabberwocky Jun 04 '21 at 14:24
  • I changed to Clion(was using codeblocks maybe it had a problem somewhere) and now i can use the string fuctions,so i got my problem solved.Is the post bad?, should i take it down ? or keep it here? – Jesepy Jun 04 '21 at 14:42

1 Answers1

3

string is a class, so you can use its member functions to manage it. See the documentation.

To remove characters, use std::erase (see this answer).

#include <iostream>
#include <string>
#include <algorithm>

int main() {
    // Create array of 10 strings
    std::string array[10];
    array[0] = "apple_.Tree.";

    std::cout << array[0].size() << "\n";

    array[0].erase(std::remove(array[0].begin(), array[0].end(), '.'), array[0].end());
    array[0].erase(std::remove(array[0].begin(), array[0].end(), '_'), array[0].end());

    std::cout << array[0];

    return 0;
}
VLL
  • 9,634
  • 1
  • 29
  • 54
  • I dont know why but those functions dont work for me – Jesepy Jun 04 '21 at 13:03
  • 1
    @Jesepy if they don't work for you something is very wrong with your system or you are not doing what is in the code here. Perhaps you are still using `new` with a size of 0 instead of this array of 10 elements. – drescherjm Jun 04 '21 at 13:59
  • I just changed to Clion from Codeblocks and i can use the string functions now,so i think ill manage now.My codeblocks probably had a problem. – Jesepy Jun 04 '21 at 14:39