Escape all % characters in a string; % is the escape character

Example : Input : India has GDP of 10% in 2009 Output : Input has GDP of 10%% in 2009 It is usually not be possible to do this in-place since the original unescaped string may not have enough space in the array to store the additional escape characters. But if we create a new array, O(n) algo is possible, though space complexity will increase. String escapePercent(String input) { StringBuilder sb = new StringBuilder(); char str = input. [Read More]

Implementing strcpy

char *strcpy ( char *dest, const char *src ); strcpy is short for string copy, which means it copies the entire contents of src into dest. The contents of dest after strcpy will be exactly the same as src such that strcmp ( dest, src ) will return 0. size\_t strlen ( const char \*s ); ```strlen will return the length of a string, minus the termating character ('\\0'). The size\_t is nothing to worry about. [Read More]

Implementing strcat

char \*strcat ( char \*dest, const char \*src );
```strcat is short for string concatenate, which means to add to the end, or append. It adds the second string to the first string. It returns a pointer to the concatenated string. Beware this function, it assumes that dest is large enough to hold the entire contents of src as well as its own contents.

Implementing strcmp

int strcmp ( const char \*s1, const char \*s2 );
```strcmp will accept two strings. It will return an integer. This integer will either be:  

Negative if s1 is less than s2.
Zero if s1 and s2 are equal.
Positive if s1 is greater than s2.

Reverse a String using bits

Question: Reverse a string in C using as little additional memory as possible. Answer: The first solution needs the size of a char and size of two integers, all of which will be allocated from the stack. This solution is the most commonly accepted “good” solution. Here is the code. Method 1 - Normal Reversal of String void reverseString(char\* str) { int i, j; char temp; i\=j\=temp\=0; j\=strlen(str)\-1; for (i\=0; i<j; i++, j\-\-) { temp\=strii; strii\=strjj; strjj\=temp; } } Method 2 - Using xor to swap 2 characters [Read More]