Removing beggning of the string

I've got big char array which contains text from somewhere else, e.g:
name Uriziel
How can I remove "name " from this array?
Try with memmove http://www.cplusplus.com/reference/clibrary/cstring/memmove/
(using C++ strings is easier as thy have the erase method)
Hi Uriziel!
Bazzy is right.You should use strings for char variables that change in size, it's easier.But if you really must use arrays of chars then this is way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cstring>

using namespace std;


int main()
{
  char my_text[20] = "name Uriziel"; // an array of chars
  int n=0; // the nuimber of first letters you want to delete
  int text_size = 0; // will store the length of my_text just for comfort
  int i=0;

  cout<<"How many of the first letters do you want do delete?"<<endl;
  cin>>n;

  text_size = strlen(my_text); // make n have the value of the number of elements of my_text

  for(i=0; i<text_size - n; i++) // this is where the word changes
  {
    my_text[i] = my_text[i+n]; // make every letter be the nth letter after it
  }

  cout<<"The new text is '"<<my_text<<"'"<<endl; // still not good, just for example

  my_text[ text_size - n ] = NULL; // this is very important, it makes another caracter NULL,
                                                     // the character after the letters you want to display

  cout<<"The good text is '"<<my_text<<"'";


  return 0;

}

A very important line is my_text[ text_size - n ] = NULL;.As you know cout stops at the first NULL it finds.That's why the first cout still doesn't work because the NULL is still at the end of the array, it's the last element of the array.But if you make another letter NULL too, then cout will stop at the first one, even if now there are 2 NULLs.
The program output for n=5 is:

How many of the first letters do you want do delete?
5
The new text is 'Urizieliziel'
The good text is 'Uriziel'

But this is just a trick.The array still has the same number of elemnets.The elements are not deleted from the memory.They still occupy space.But cout stops at the first NULL so it gives the illusion that the array is smaller.
This is a bad method because the array still occupies the same amount of memory.You could use malloc, realloc, etc. to solve it but it really complicates things.Still, if you have to have an array of chars, that would be the way to do it, to also use malloc, etc. to correctly allocate and deallocate the array and the elements you no longer use.
Topic archived. No new replies allowed.