1 / 14

Complementary tutorial 2 – strings++

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.

keeley
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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Complementary tutorial 2 – strings++ Prepared by: Valentin Kravtsov

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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; }

  8. 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; }

  9. Compile/not compile questions

  10. Compile/not compile questions

  11. Compile/not compile questions

  12. Compile/not compile questions

  13. Given an array of characters of size n, and a new “order”, find if it is “newly ordered”.

  14. 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.

More Related