You are given a string which contains some special
characters. You also have set of special characters. You are
given other string (call it as pattern string). Your job is
to write a program to replace each special characters in
given string by pattern string. You are not allowed to
create new resulting string. You need to allocate some new
memory to given existing string but constraint is you can
only allocate memory one time. Allocate memory exactly what
you need not more not less.

Answer Posted / abdur rab

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

/**
* int copy ( char* str, char* _pattern_to_copy, int
number_of_bytes_moved, int total_size_allocated, int
diff_size )
* @param str, the pointer from where the pattern starts
* @param _pattern_to_copy, what needs to be placed insted
of the pattern
* @param number_of_bytes_moved, the number of bytes at
which the pattern first appeared
* @param total_size_allocated, the total size the space
has been allocated (eliminating the space for NULL)
* @param diff_size, the difference between the pattern and
the string that needs to be replaced
*
*/
int copy ( char* str, char* _pattern_to_replace, int
number_of_bytes_moved, int total_size_allocated, int
diff_size )
{
int _loop = 0;

/**
* Starting from end, move towards untill the
pattern appears
* start point is array start + the
number_of_bytes_moved (first apperance of the pattern)
* so the end point should be array end -
number_of_bytes_moved (the last space before the NULL
character
* move the array down to create space for the
string to be replaced
*/
for ( _loop = total_size_allocated; (
total_size_allocated - number_of_bytes_moved ) >
diff_size; total_size_allocated-- )
{
str [ total_size_allocated -
number_of_bytes_moved ] = str [ total_size_allocated -
number_of_bytes_moved - diff_size ];
}

/**
* Replace the string on the pattern
* Use memcpy since it does not copy provide NULL
by itself
*/
memcpy ( str, _pattern_to_replace, strlen (
_pattern_to_replace ) );

return ( 0 );
}

int main ( int argc, char* argv [] )
{
char* string_value = NULL;
char pattern_to_replace [] = {"replacement"};
char pattern_2b_replaced [] = {"#"};
int _count = 0;
char* _ptr = NULL;
int alocation_size = 0;
int number_of_bytes_moved = 0;
int old_strlen = 0;

/**
* Allocate the size to hold the orignal string
* copy the required string with the pattern
*/
string_value = ( char* ) malloc ( 26 * sizeof (
char ) );
if ( NULL != string_value )
{
strcpy ( string_value, "Hi # of new #
string by #" );
}

/**
* Count the number of occurences of the pattern.
*/
_ptr = string_value;
while ( _ptr = strstr ( _ptr,
pattern_2b_replaced ) )
{
_ptr += 1;
_count++;
}

/**
* Calculate the size of the new string that needs
to be accomadated
* the number of times the pattern occurs in the
original string
*/
alocation_size = ( strlen ( pattern_to_replace ) -
strlen ( pattern_2b_replaced ) ) * _count; // calculated
size of new string
alocation_size += strlen ( string_value );
// add the size of old
string
old_strlen = strlen ( string_value ) + 1;
// allocate space to
store NULL

/**
* Increase the space in the old pointer using
realloc
* and copy '&' in the newly allocated spaces alone
* just for the sake of our reference.
*
* Remember to exclude the 1 which we allocated for
the NULL character
*/
string_value = ( char* ) realloc ( string_value, (
alocation_size * sizeof ( char ) ) );
memset ( ( string_value + old_strlen) , '&', (
alocation_size - old_strlen - 1 ) );

/**
* Store the NULL at the end of the total allocated
size
* we use alocation_size - 1
*
* eg: a[5] means 6th location in the array
* If we want to store at the 6th location
* we need to provide 5 (ie. alocation_size - 1)
*/
string_value [ alocation_size - 1 ] = '\0';

/**
* Replace the string needs to be filled
*/
_ptr = string_value;
while ( _ptr = strstr ( _ptr,
pattern_2b_replaced ) )
{
number_of_bytes_moved = ( _ptr -
string_value );
copy ( _ptr, pattern_to_replace,
number_of_bytes_moved, ( alocation_size - 1 ),
( strlen (
pattern_to_replace ) - strlen ( pattern_2b_replaced ) ) );
_ptr += 1;
}


printf ( "\n string :%s, no of occurence :%d,
size :%d", string_value, _count, alocation_size );

free ( string_value );
}

Is This Answer Correct ?    0 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why c language?

654


What is scope of variable in c?

573


An expression to whose value an operater is applied a) operand b) variable c) constant d) all of the above

665


write a programe to accept any two number and check the following condition using goto state ment.if a>b,print a & find whether it is even or odd and then print.and a

1457


WHAT IS THE DEFINATION OF IN TECHNOLOGY AND OFF TECHNOLOGY ?

1866






What is 1d array in c?

608


Is c# a good language?

614


What is the use of ?: Operator?

671


What is structure of c program?

612


Where does the name "C" come from, anyway?

651


Explain Basic concepts of C language?

652


how to construct a simulator keeping the logical boolean gates in c

1737


Is c procedural or object oriented?

586


the real constant in c can be expressed in which of the following forms a) fractional form only b) exponential form only c) ascii form only d) both a and b

1915


Write a C Program That Will Count The Number Of Even And Odd Integers In A Set using while loop

1719