-1

I'm having an issue removing characters from a string. I have been able to identify the characters to remove and store them as a new string but I would like a code that enables me to remove the new string's characters from the original string (where I've labelled as xxxxxxxx).

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

char input[120], to_del[120], new_input[120];
int a,b;


void removeString();

int main(void)
{
    printf("\nEnter your sentence: ");
    gets(input);

    printf("\nWhere to start: ");
    scanf("%d",&a);
    printf("\nHow many characters to delete: ");
    scanf("%d",&b);

    removeString();

    printf("\nNew sentence: %s\n",new_input);

    return ;
}

void removeString()
{
    memcpy(to_del, &input[a-1], b);
    new_input [strlen(input)-b] =xxxxxxxx;
    return ;
}
halfer
  • 19,824
  • 17
  • 99
  • 186

3 Answers3

0

You can take following approach

  1. copy from index 0 to a-1 of input to new_input
  2. copy from index a+b-1 to end of input to new_input

Finaly, your new_input will have all the values between position a and b removed.

Note:

  1. using gets() is dangerous. Use fgets() instead.
  2. You cannnot assign strings using = in c. You need to copy the string containts using strcpy()/memcpy().
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • fgets needs more arguments plus everything is working fine right now. Out of curiousity, in which situation could gets() be dangerous – Nathan Piper May 07 '15 at 11:31
  • @NathanPiper see the answers in [this question](http://stackoverflow.com/q/3302255/2173917) for a detailed discussion on the same. – Sourav Ghosh May 07 '15 at 11:34
0

EDIT in response to @SouravGhosh comment, about overlapping strings. First I copy the string to new_input and then copy the right hand part back to input to overwrite the section which must be removed.

int len = strlen(input);
strcpy(new_input, input);
if (a < len && a+b <= len)
    strcpy (input + a, new_input + a + b);

I notice the rest of your code doesn't check anything is happening correctly, such as the return value from scanf, and please note gets is dangerous: it is better to use fgets with stdin.

The strcpy instruction copies the right-hand part of the string over the part you want removed. It works by using pointer arithmetic. Adding a to input is the same as getting the address of input[a], the place where you want the removal to begin. Similary new_input[a+b] is the start of the right hand part you wish to keep.

The point of the if condition, is that if a isn't within the string, or if the part you want removed overflows the end of the string, it can't be done.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • please explain the strcpy statement and how the if condition affects it. I'm really new to c programming. Also, doesn't 'void' at the start of each function mean that a return value becomes redundant? – Nathan Piper May 07 '15 at 10:57
  • heh? Please correct me if I'm wrong, but isn't this (as shown in your code) the ideal way of source and destination buffer overlap? – Sourav Ghosh May 07 '15 at 11:02
  • @SouravGhosh yes you are right - it's supposed to be Undefined Behaviour, so `memmove` should be used. But I have always assumed the UB applies when the strings overlap the other way. – Weather Vane May 07 '15 at 11:08
  • @NathanPiper I amended the answer, and explained more. – Weather Vane May 07 '15 at 11:14
0
void removeString(void);

int main(void){
    printf("\nEnter your sentence: ");
    scanf("%119[^\n]%*c", input);

    printf("\nWhere to start(1 origin): ");
    scanf("%d", &a);
    printf("\nHow many characters to delete: ");
    scanf("%d", &b);

    removeString();

    printf("\nOriginal sentence: %s\n", input);
    printf("\nNew sentence: %s\n", new_input);
    printf("\nLabelled sentence: %s\n", to_del);

    return 0;
}

void removeString(void){
    strcpy(new_input, input);
    memmove(&new_input[a-1], &new_input[a -1 + b], strlen(&new_input[a -1 + b])+1);

    strcpy(to_del, input);
    memset(&to_del[a-1], 'x', b);
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • Thank you but I should have said originally that I require the code without memmove. – Nathan Piper May 07 '15 at 11:13
  • @NathanPiper `memcpy(new_input, input, &input[a] - input - 1); memcpy(&new_input[a-1], &input[a -1 + b], strlen(&input[a -1 + b])+1);` – BLUEPIXY May 07 '15 at 11:20