delete the first character in a character array, But keep the wrest

How do i delete the first character in a character array?
I want output to be

Str = 'Hello'

After calling deletefirst

Str = 'ello'



1
2
3
4
5
6
void DeleteFirst (char S[ ])
{

	for( int i = 1; i < MAX_LENGTH; i++)
		cout << S[i];
}


i am just writing the wrest of the Array without deleting the first. how do i delete the first?
Last edited on
closed account (j3Rz8vqX)
If using dynamic array:
Make an array of characters shorter than the prior by one and copy over all members other than the one expected to be deleted.

If not using dynamic array:
Shuffle all member an index prior to themselves, after the unit to be deleted is found, that is. Blank out the last spot.
now i must figure out how to do this
Last edited on
- line 4 initializes i to 0

- line 6 then assigns i to 0 (even though it already is zero). You probably meant == here, but even that wouldn't do much.

- line 7 is never executed because i=0 will always be 0 (false)

- line 8 is not part of your while loop because you didn't use braces. So it comes after the loop. It increments i so now i=1

- line 9 and 10 are good, and reassigns i to 1. So pretty much everything before line 9 does absolutely nothing and can be removed.

This is the same:

1
2
3
4
5
void DeleteFirst (char S[ ])
{
    for( int i = 1; i < MAX_LENGTH; i++)
        cout << S[i];
}


Although you can make it even simpler by printing the whole string starting with the 2nd character with some pointer math. Rather than printing it one character at a time like you are above:

1
2
3
4
void DeleteFirst (char S[ ])
{
    cout << (S+1);
}



It's also worth nothing that none of this does what the function title suggests. IE: you are not deleting the first character, you are merely printing the string passed the first character.
i understand. i need to copy the other parts of the character array over the one before them so the first is deleted. i am trying to figure out how. thanks for the reply
Your DeleteFirst() function doesn't look right.

You use an assignment (i = 0) as the condition instead of a comparison of some kind. This is usually a sign of a coding mistake.

In addition, i++ is outside the while() loop. This is C++, not Python. Indentation doesn't affect the meaning of the code.

Here's what you probably wanted to write:

1
2
3
4
5
while (i == 0)
{
    S[i] = 0;
    i++;
}


That said, your code only sets the first character to 0, and prints the string starting from the second element.

Noob Programmer wrote:
can this be simplified?

Yes, you could simply use a pointer to the second character, and print it.
(The original string will not be changed.)

1
2
3
4
5
char msg[] = "Hello";
const char *p = msg + 1;

std::cout << p << std::endl; // if using C++
printf("%s\n", p); // if using C 


If using C++ you are advised to use std::string instead of old fashioned char arrays.
Then you could be using std::string::substr(), among other techniques, to get a "headless" new string.
http://www.cplusplus.com/reference/string/string/substr/

If using C, you can use memmove() to copy the string onto itself.
http://www.cplusplus.com/reference/cstring/memmove/

1
2
3
4
5
6
7
#include <string.h>

// ...

char msg[] = "Hello";

memmove(msg, msg + 1, sizeof msg - 1);

Last edited on
Thanks for the reply,

i Changed my code to this

1
2
3
4
5
6
void DeleteFirst (char S[ ])
{

	for( int i = 1; i < MAX_LENGTH; i++)
		cout << S[i];
}


but i understand that above they mentioned i am only printing the the wrest of the Array and not really deleting the first.
Do i use Swap and switch the characters till the first becomes the last and get rid of it like Dput said? he did not mention Swap though
closed account (j3Rz8vqX)
Example of 100% copying values:
1
2
3
4
    for(int i=0;i<i_size;i++)//Don't step out of bound(Less than max size)
    {
        i_array[i]=t_array[i];//Copy values : exactly like an array.
    }

In your case you'd want to copy everything except a selected.

Example of copying from a larger array, and excluding the "to be deleted":
1
2
3
4
5
6
7
8
    for(int i=0,j=0;i<i_size;i++,j++)//Don't step out of bound(Less than max size)
    {
        if(t_array[j]==toBeDeletedValue)
        {
            j++;//Assuming j is the larger array; this adjusts a skip for the j-indexed array;
        }
        i_array[i]=t_array[j];//Copy values : exactly like an array.
    }

In the above case, j best be larger than i or else there will be conflicts.

Hopefully the examples help.

It was modified from the browser and abstracted from another post:
http://www.cplusplus.com/forum/general/130858/
Topic archived. No new replies allowed.