Joel Youngblood
Joel Youngblood

Reputation: 13

C function to remove string

Total beginner here trying to wrap my head around some basic C knowledge before taking an in-depth Objective-C course in few weeks, so I apologize if this is pedestrian..

The assignment is to create a function that takes three arguments: a source string, an integer to start deletion from that string, and another integer that tells the function how many characters to delete. The example as given in the book is the starting string "the wrong son" and then 4 and 6, leaving just "the son".

My thinking is to create two separate strings based on the first word and second, then concatenate them together. So far, I'm only able to extract "the", and my second for loop apparently does nothing. This is what I have so far:

#include<stdio.h>

int main (void)
{
    char text[]="the wrong son";
    void removeString (char[], int, int);
    int s, l;

    printf("Enter starting point for removal: ");
    scanf("%d",&s);

    printf("\nEnter lenght of removal: ");
    scanf("%d",&l);

    removeString(text, s, l);

    printf("\nString after removal = %s",text);

    return 0;
}

void removeString (char s1[], int s, int l)
{

    printf("s1 = %s, s=%d, l=%d\n", s1, s, l);

    char s2[]="";
    char s3[]="";

    int i,j;

    for (; i<s; ++i)
        {
            s2[i]=s1[i];
        }

    printf("\n");

    for (; j>l; ++j)
        {
            s3[j]=s1[j];
        }

    printf("s2 = %s, s3 = %s", s2, s3);
}

I'm properly confused. Any help would be much appreciated.

Upvotes: 1

Views: 5338

Answers (5)

SzG
SzG

Reputation: 12619

// This will edit string s1 in place
void removeString(char *s1, int s, int l)
{
  int len = strlen(s1);
  // Abort if start pos beyond end of string, or nothing to remove
  if (s >= len || l < 1)
    return;
  // Terminate original string at pos s
  s1[s] = 0;
  // Finished if s+l beyond end of string
  if (s+l >= len)
    return;
  // Otherwise concat rest to 1st part
  strcat(s1, s1+s+l);
}

Upvotes: 0

Jo&#235;l Hecht
Jo&#235;l Hecht

Reputation: 1842

In C, strings are not objects, they are just arrays of chars in memory.

In your case, removing a part of the string can be done by a simple memory move:

void removeString (char string[], int start, int length)
{
    int from = start + length;

    // You should do some verifications here, like start < string length, etc. 
    while (string[from] != 0)
        string[start++] = string[from++];
    string[start] = 0;
}

Upvotes: 1

Michael Laszlo
Michael Laszlo

Reputation: 12239

Here is a simpler implementation:

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

void removeString (char text[], int start, int length) {
  int n = strlen(text), i;
  for (i = start+length; i <= n; i++) {
    text[i-length] = text[i];
  }
}


int main (void) {
  char text[] = "the wrong son";
  int start, length;
  printf("Original text: \"%s\"\n", text);

  printf("Enter starting point for deletion: ");
  scanf("%d", &start);
  printf("\nEnter number of characters to delete: ");
  scanf("%d", &length);

  removeString(text, start, length);
  printf("After deletion: \"%s\"\n", text);

  return 0;
}

Upvotes: 0

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

This function

void removeString (char s1[], int s, int l)
{

printf("s1 = %s, s=%d, l=%d\n", s1, s, l);

char s2[]="";
char s3[]="";

int i,j;

for (; i<s; ++i)
{
    s2[i]=s1[i];
}

printf("\n");

for (; j>l; ++j)
{
    s3[j]=s1[j];
}

printf("s2 = %s, s3 = %s", s2, s3);
}

Has a two important problems,

  1. You didn't allocate enough space for s2 or s3.

  2. You never initialize i or j.

Try this instead

void removeString (char *source, int offset, int length)
{
    int    i;   
    size_t source_length;
    if (source == NULL)
        return;
    source_length = strlen(source);
    if ((offset >= source_length) || (source_length - offset < length))
        return;
    printf("source = %s, offset = %d, length = %d\n", source, offset, length);
    for (i = 0 ; i < offset ; ++i)
        result[i] = source[i];    
    for (j = 0 ; source[j + offset] != '\0' ; ++i)
       result[j] = source[j + offset];  
    printf("s2 = %s\n", result);
}

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134336

Creating separate arrays for this purpose is not the best approach, IMHO. Instead, an altogether better an cleaner approach will be

  1. Receive the array, start index and skip index values.
  2. calculate the length of the array using strlen().
  3. put the value in (start index+skip index) location to start index location.
  4. increment the start index by 1.
  5. continue the step 3 and 4 until the array length runs out, i.e., the latest (start index+skip index) points to the last element in the array.

Upvotes: 2

Related Questions