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 are the main characteristics of c language describe the structure of ac program?

0 Answers  


Draw a flowchart to produce a printed list of all the students over the age of 20 in a class .The input records contains the name and age of students. Assume a sentinel value of 99 for the age field of the trailer record

0 Answers   Wipro,


Hi Every one...........I have been selected for the SBI Clerk. But i m one month Pregnanat. So anyone please suggest me, is they take any objection on my joining .

4 Answers   State Bank Of India SBI,


How pointer is benefit for design a data structure algorithm?

2 Answers  


What is the output of the following progarm? #include<stdio.h> main( ) { int x,y=10; x=4; y=fact(x); printf(ā€œ%d\nā€,y); } unsigned int fact(int x) { return(x*fact(x-1)); } A. 24 B. 10 C. 4 D. none

2 Answers  






Give basis knowledge of web designing ...

0 Answers   HCL,


why we use "include" word before calling the header file. is there any special name for that include??????

1 Answers   TCS,


Write a program that takes a 5 digit number and calculates 2 power that number and prints it.

1 Answers  


What is a void * in c?

0 Answers  


What is uint8 in c?

0 Answers  


How can you allocate arrays or structures bigger than 64K?

0 Answers  


Write a program to print factorial of given number using recursion?

0 Answers  


Categories