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
Is the exit() function same as the return statement? Explain.
What is difference between union and structure in c?
Write a code to generate a series where the next element is the sum of last k terms.
What is a pointer value and address in c?
What is an lvalue?
How we can insert comments in a c program?
How to check whether string is a palindrome, WITHOUT USING STRING FUNCTIONS?
Draw a diagram showing how the operating system relates to users, application programs, and the computer hardware ?
What are variables c?
Which is better pointer or array?
Is printf a keyword?
Why isn't it being handled properly?
Why c language?
Explain logical errors? Compare with syntax errors.
What is merge sort in c?