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.

Answers were Sorted based on User's Feedback



You are given a string which contains some special characters. You also have set of special charact..

Answer / raja

no this answer is not correct

Is This Answer Correct ?    0 Yes 0 No

You are given a string which contains some special characters. You also have set of special charact..

Answer / 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

More C Interview Questions

What is 'bus error'?

0 Answers  


what are the advantage of pointer variables? write a program to count the number of vowels and consonants in a given string

3 Answers  


When should structures be passed by values or by references?

0 Answers   Adobe,


write a c program to add two integer numbers without using arithmetic operator +

13 Answers   Value Labs,


What is the scope of global variable in c?

0 Answers  






Prove or disprove P!=NP.

5 Answers   Microsoft,


What is the memory allocated by the following definition ? int (*x)[10];

4 Answers   ADITI, Wipro,


What is #include stdio h?

0 Answers  


15.what is the disadvantage of using macros? 16.what is the self-referential structure? 17.can a union be self-referenced? 18.What is a pointer? 19.What is the Lvalue and Rvalue? 20.what is the difference between these initializations? 21.Char a[]=”string”; 22.Char *p=”literal”; 23.Does *p++ increment p, or what it points to?

2 Answers   CTS,


write a c program to print the next of a particular no without using the arithmetic operator or looping statements?

1 Answers   TCS,


could u able to tell about suresoft technical session

1 Answers  


what is dangling pointer?

1 Answers   LG Soft,


Categories