Difference between revisions of "Interview Preparation Strings"
From Embedded Systems Learning Academy
					
										
					
					| Proj user14 (talk | contribs)  (→Reverse words in a string) | Proj user14 (talk | contribs)   (→Reverse words in a string) | ||
| Line 15: | Line 15: | ||
| <BR/> | <BR/> | ||
| == Reverse words in a string == | == Reverse words in a string == | ||
| − | |||
| If string to be reversed is "where there is a will there is a way" | If string to be reversed is "where there is a will there is a way" | ||
| Output needed : "way a is there will a is there where" | Output needed : "way a is there will a is there where" | ||
| − | |||
| <BR/> | <BR/> | ||
| To reverse the words in a string , we will follow below steps: | To reverse the words in a string , we will follow below steps: | ||
| Line 25: | Line 23: | ||
| <BR/> | <BR/> | ||
| 2) Second, we will reverse this string by letter to get the desired output. | 2) Second, we will reverse this string by letter to get the desired output. | ||
| − | < | + | <BR/> | 
| − | BR/> | ||
| We are going to use previous reverse_str function with one more parameter length as below: | We are going to use previous reverse_str function with one more parameter length as below: | ||
| <syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
| + | |||
| void reverse_str(char *s ,int len) | void reverse_str(char *s ,int len) | ||
| { | { | ||
| − | + |     int i,j; | |
| 	char temp; | 	char temp; | ||
| 	j= len - 1; | 	j= len - 1; | ||
| − | 	int mid =  | + | 	int mid = len/2; | 
| 	for(i=0;i<mid;i++,j--) | 	for(i=0;i<mid;i++,j--) | ||
| 	{ | 	{ | ||
| Line 53: | Line 51: | ||
| 	 * In below while loop , string becomes "erehw ereht si a lliw ereht si a yaw" | 	 * In below while loop , string becomes "erehw ereht si a lliw ereht si a yaw" | ||
| 	 */ | 	 */ | ||
| − | 	while(* | + | 	while(*s) | 
| 	{ | 	{ | ||
| − | 		if( *( | + | 		if( *(s+j) == ' ' || *(s+j) == '\0') | 
| 		{ | 		{ | ||
| − | 			reverse(  | + | 			reverse( s+i, j-i ); | 
| 			i = j+1; | 			i = j+1; | ||
| 		} | 		} | ||
| − | 		if( *( | + | 		if( *(s+j) == '\0') | 
| 		{ | 		{ | ||
| 			break; | 			break; | ||
| Line 69: | Line 67: | ||
| 	 * 2nd Step | 	 * 2nd Step | ||
| 	 */ | 	 */ | ||
| − | 	reverse( | + | 	reverse(s,strlen(s));   | 
| } | } | ||
| </syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 21:32, 24 November 2016
Contents
Reverse a string
void reverse_str(char *s)
{
    const int len = strlen(s);
    const int mid = len / 2;
    for (int i = 0, j=len-1; i < mid; i++, j--) {
        char c = s[i];
        s[i]   = s[j];
        s[j]   = c;
    }
}
Reverse words in a string
If string to be reversed is "where there is a will there is a way"
Output needed : "way a is there will a is there where"
To reverse the words in a string , we will follow below steps:
1) First, we will reverse each word in the string as "erehw ereht si a lliw ereht si a yaw"
2) Second, we will reverse this string by letter to get the desired output.
We are going to use previous reverse_str function with one more parameter length as below:
void reverse_str(char *s ,int len)
{
    int i,j;
	char temp;
	j= len - 1;
	int mid = len/2;
	for(i=0;i<mid;i++,j--)
	{
		temp = s[i];
		s[i] = s[j];
		s[j] = temp;
	}
}
void reverse_words_in_string(char *s)
{
	int i=0;
	int j=0;
	/*
	 * 1st step
	 * In below while loop , string becomes "erehw ereht si a lliw ereht si a yaw"
	 */
	while(*s)
	{
		if( *(s+j) == ' ' || *(s+j) == '\0')
		{
			reverse( s+i, j-i );
			i = j+1;
		}
		if( *(s+j) == '\0')
		{
			break;
		}
		j++;
	}
	/*
	 * 2nd Step
	 */
	reverse(s,strlen(s)); 
}
Find the last word in a string
TODO
 
							