Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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 does c mean in basketball?

0 Answers  


write a program to add two numbers of any size.....(remember any size)

1 Answers  


21. #define square(x) x*x main() { int i; i = 64/square(4); printf("%d",i); }

3 Answers  


1. Duplicate the even numbers. -1 Sample I/O Array1:- 4,2,24,3,22 Updated Array:- 4,4,2,2,24,24,3,22,22 2. Reverse the array in a region - 1 Sample I/O Array1:- 4,2,24,3,22,41,21 Enter Region:- 2,5 Update Array:-4,41,22,3,24,2,21 3. Store first the even digits in an array and then odd digits in same array -2 Sample I/O Array1:- 4,2,24,3,22,41,21 Array 2:- 4,2,2,4,2,2,4,2,3,1,1 4. Store the count of the digits in all numbers in an array and print the count. -2 Sample I/O Array1:- 4,2,9,3,7,41,28 Array 2:- 0,1,1,1,2,0,0,1,1,1 1-1;2-1;3-1;4-2;7-1;8-1;9-1 5. Store all palindrome numbers in to another array -2 Sample I/O Array1:- 4,22,9,313,7,141,28 Array 2:- 4,22,9,313,141 6. Arrange the array in such a way that odd numbers come first and then even numbers -1 Sample I/O Array1:- 4,22,9,313,7,141,28 Update Array1:- 9,313,7,141,4,22,28 7. Store into another array by inserting it in the right place - 2 Sample I/O Array1:- 4,22,9,313,7,141,28 Array2:- 4 /4,22 /4,9,22 /4,9,22 ,313 /4,7,9,22 ,313 /4,7,9,22 ,141,313 / 4,7,9,22,28 ,141,313 8. Merge two sorted arrays in a sorted fashion - 3 Sample I/O Array 1:- 3,6,7,9,11,16 Array 2:- 1,2,5,7,20 Array 3:- 1,2,3,5,6,7,9,11,16,20 9. Upadte the array so that an array element is followed by its revere - 1 Sample I/O Array 1:- 13,63,74,9,11,16 Updated Array 1:- 13,31,63,36,74,47,9,9,11,11,16,16 10.Spread the digits of the array in the same array. -1 Sample I/O Array 1:- 13,63,74,9,11,16 Updated Array 1:-1,3,6,3,74,9,1,1,1,6 11.Shift the boundary of array to have only 2 digit numbers. Sample I/O Array 1:- 139,643,74,9,101,126 Updated Array 1:- 13,96,43,74,91,11,26 12.Print the largest occuring digit in an array of N numbers. Sample I/O Array 1:- 13,63,74,9,11,16 1 occurs most.

2 Answers   Intel,


What do you mean by Recursion Function?

0 Answers   Hexaware,


What are logical errors and how does it differ from syntax errors?

0 Answers  


Why do we use header files in c?

0 Answers  


In which area global, external variables are stored?

3 Answers  


What is header file definition?

0 Answers  


How the C program can be compiled?

11 Answers   HP,


Are global variables static in c?

0 Answers  


What is declaration and definition in c?

0 Answers  


Categories