Answer to Question #52420 in C for sara

Question #52420
Write a function called removeString to remove a specified number of characters from a character string. The function should take three arguments: the source string, the starting index number in the source string, and the number of characters to remove. So, if the character array text contains the string "the wrong son", the call
removeString(text,4,6);
has the effect of removing the characters “wrong“ (the word “wrong” plus the space that follows) from the array text. The resulting string inside text is then "the son".
1
Expert's answer
2015-06-04T03:39:12-0400
/* Answer on Question#52420 - Subject - Programming, C++ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int removeString(char* ch, int s, int n )
{
    
   char *buf = new char[256]; /* memory for result string*/
   char *buf_p = buf;         /* pointer to beginning of buf*/
   memset(buf,0,256);         /* to zero memory*/
    char *ch_p = ch;          /*pointer to beginning of ch*/
    int length = 0;
    while ( *ch_p != '\0' )   /* defines length of string*/
    {
          length++;
          ch_p++;
    }
    
    /* copying needed chars from ch to result string*/
    for (int i=0; i<=s-1; i++)
    {
        *buf = *ch;
        ch++;
        buf++;
    }
   
    ch = ch+n*sizeof(char);    /* increment ch by n */
    for (int i=n; i<=length; i++)
    {
        *buf = *ch;
        ch++;
        buf++;
    }
    printf("%s\n",buf_p); /*print result*/
    return 0;
}
int main()  /* main function*/
{
    char ch[] = "the wrong son"; /*string for test*/
removeString(ch,4,6);  /* func.call*/
system("pause");
return 0;
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS