complementary tutorial 2 strings n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Complementary tutorial 2 – strings++ PowerPoint Presentation
Download Presentation
Complementary tutorial 2 – strings++

Loading in 2 Seconds...

play fullscreen
1 / 14

Complementary tutorial 2 – strings++ - PowerPoint PPT Presentation


  • 117 Views
  • Uploaded on

Complementary tutorial 2 – strings++. Prepared by: Valentin Kravtsov. char str[] = "abcdefghkl"; int i,str_len = strlen(str); for(i=0 ; i<str_len/2 ; i++){ swap(str+i , str+str_len – 1 – i ); }. Given a string, reverse it. Output: lkhgfedcba.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

Complementary tutorial 2 – strings++


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
complementary tutorial 2 strings

Complementary tutorial 2 – strings++

Prepared by: Valentin Kravtsov

slide2

char str[] = "abcdefghkl";

int i,str_len = strlen(str);

for(i=0 ; i<str_len/2 ; i++){

swap(str+i , str+str_len – 1 – i );

}

  • Given a string, reverse it

Output:

lkhgfedcba

slide3
Given string of size n, rotate it m places to the right: No new arrays are allowed. O(nm)

abcdefghk => fghkabcde (n=9, m=4).

char tmp,str[] = "abcdefghk";

int i,j, m=4, n = strlen(str);

for(i=0;i<m;i++){

tmp = str[n-1];

for(j=n-2;j>=0;j--) { str[j+1] = str[j]; }

str[0] = tmp;

}

Output:

fghkabcde

slide4
Given string of size n, rotate it m places to the right: (n=9, m=4). The efficient version. O(n)

abcdefghk => fghkabcde

The algorithm:

  • Identify the new first letter: “f”
  • Reverse both parts: edcbakhgf
  • Reverse the entire string: fghkabcde
the code
The code:

void reverse(char* str, int from, int to){

int i, half_len = (to-from+1)/2;

for(i=0;i<half_len;i++){

swap(str+from+i, str+to-i);

}

}

char str[] = "abcdefghk";

int i, m=4, n = strlen(str), break_point = n-m;

reverse(str,0,break_point-1);

reverse(str,break_point,n-1);

reverse(str,0,n-1);

Output:

fghkabcde

slide6
Given string of with several words, reverse the order of words, (not characters), no additional arrays/strings are allowed. O(n)

ima aba and bamba => bamba and aba ima

The algorithm:

  • Reverse the whole string:

abmab dna aba ami

  • Reverse the letters in each word:

bamba and aba ima

slide7
Given string of with several words, reverse the order of words, (not characters), no additional arrays/strings are allowed.

ima aba and bamba => bamba and aba ima

char tmp,str[] = "ima aba and bamba";

int end,start=0, n = strlen(str);

reverse(str,0,n-1);

while( find_next_word(str,&start,&end) ){

reverse(str,start,end);

start=end+1;

}

slide8
Finds next word by updating start and end to appropriate values. Return true/false (1/0) if a new word is found.

int find_next_word(char* str, int* start, int* end){

for( ; str[*start] && str[*start]==' '; (*start)++){}

if( !str[*start] ) return 0;

for(*end = *start ; str[*end+1] &&

str[*end+1] !=' ' ; (*end)++){}

return 1;

}

given an array of characters of size n and a new order find if it is newly ordered
Given an array of characters of size n, and a new “order”, find if it is “newly ordered”.
your very challenging homework
Your very challenging homework…
  • Given an array of integers of size n, where one number appears more then n/2 times. Find this number, by scanning the array one time only.