Advertisement
Mizuhara_Chizuru

Coding Ninja Surya's Answers Lecture 10: Character Arrays and 2D Arrays

Jan 18th, 2022
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.66 KB | None | 0 0
  1. #include<string.h>
  2. bool checkPalindrome(char str[])
  3. {
  4.     int i=0,j=strlen(str)-1;
  5.     while(j>i)
  6.     {
  7.         if(str[i]!=str[j])
  8.             return false;
  9.         i++;
  10.         j--;
  11.     }    
  12.     return true;
  13. }
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25. #include<string.h>
  26. void replaceCharacter(char input[], char c1, char c2) {
  27.     int i=0;
  28.     int j = strlen(input);
  29.     for(i; i < j;i++){
  30.         if(input[i] == c1){
  31.             input[i]=c2;
  32.         }
  33.     }
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. #include<string.h>
  46. void trimSpaces(char input[]) {
  47.  
  48.     int count = 0;
  49.     int len = strlen(input);
  50.     for(int i = 0; input[i]; i++){
  51.         if(input[i] != ' '){
  52.             input[count]=input[i];
  53.             count++;
  54.         }
  55.     }
  56.     input[count] = '\0';
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. int length(char input[])
  68. {
  69.     int len = 0;
  70.     for(int i = 0; input[i] != '\0'; i++)
  71.     {
  72.          len++;
  73.     }
  74.         return len;
  75. }
  76. void reverse(char input[], int start, int end){
  77.     int i = start,
  78.     j = end;
  79.     while(i < j)
  80.     {
  81.         char temp = input[i];
  82.         input[i] = input[j];
  83.         input[j] = temp;
  84.         i++;
  85.         j--;
  86.     }
  87. }
  88.  
  89. void reverseCompleteString(char input[]) {
  90.     int i = 0,
  91.     j = length(input) - 1;
  92.     while(i < j) {
  93.         char temp = input[i];
  94.         input[i] = input[j];
  95.         input[j] = temp;
  96.          i++;
  97.         j--;
  98.     }
  99. }
  100. void reverseStringWordWise(char input[]){
  101.     reverseCompleteString(input);
  102.     int start = 0,
  103.     end = 0, i;
  104.     for(i = 0; input[i] != '\0'; i++){
  105.          if(input[i] == ' '){
  106.             end = i - 1;
  107.             reverse(input, start, end);
  108.             start = i + 1;
  109.         }
  110.     }
  111. end = i - 1;
  112. reverse(input, start, end);
  113. }
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127. #include<cstring>
  128. void printSubstrings(char str[]){
  129.     for(int start = 0; str[start] != '\0'; start++){
  130.         for(int end = start; str[end] != '\0'; end++){
  131.             for(int i = start; i <= end; i++){
  132.                 cout << str[i];
  133.             }
  134.             cout << endl;
  135.         }
  136.     }
  137. }
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. #include<iostream>
  150. using namespace std;
  151. int main(){
  152.     int row, col;
  153.     cin>>row>>col;
  154.     int arr[row][col];
  155.     for(int i = 0; i < row; i++){
  156.         for(int j = 0; j < col; j++ ){
  157.             cin>>arr[i][j];
  158.         }
  159.     }
  160.     int j = 0;
  161.     while(j < col){
  162.         int i = 0;
  163.         int col1 = 0;
  164.         for(i; i < row; i++){
  165.             col1 = col1 +arr[i][j];
  166.         }
  167.         j++;
  168.         cout<< col1 << " ";
  169.     }
  170. }
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182. #include<climits>
  183. void findLargest(int **input, int nRows, int mCols){
  184.         int i,j,sum = 0;
  185.         int tempr = INT_MIN;
  186.         int maxrow = 0, maxr = 0;
  187.     // finding each nRows sum
  188.     for(i = 0; i < nRows; i++){
  189.         for (j = 0; j < mCols; j++) {
  190.             // Add the element
  191.             sum = sum + input[i][j];
  192.         }
  193.         //finding th greatest nRows
  194.         if(sum > tempr){
  195.             maxrow = sum;
  196.             maxr = i;
  197.             tempr = maxrow;
  198.         }
  199.         // Reset the sum
  200.         sum = 0;
  201.     }
  202.     i=0,j=0,sum = 0;
  203.     int tempc = INT_MIN;
  204.     int maxcol = 0, maxc = 0;
  205.    
  206.     // finding the each mColsumn sum
  207.     for (i = 0; i < mCols; ++i) {
  208.         for (j = 0; j < nRows; ++j) {
  209.  
  210.             // Add the element
  211.             sum = sum + input[j][i];
  212.         }
  213.         // finding th greatest mColsumn
  214.         if(sum > tempc){
  215.             maxcol = sum;
  216.             maxc = i;
  217.             tempc = maxcol;
  218.         }
  219.         // Reset the sum
  220.         sum = 0;
  221.     }
  222.     //comparing the max nRows or mCols
  223.     if(maxrow > maxcol){
  224.         cout<<"row " << maxr<<" "<<maxrow;
  225.     }
  226.     else if(maxrow < maxcol){
  227.         cout<<"column " << maxc<<" "<<maxcol;
  228.     }
  229.     else{
  230.         cout<<"row " << maxr<<" "<<maxrow;
  231.     }
  232. }
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250. #include <iostream>
  251. using namespace std;
  252. void wavePrint(int **input, int nRows, int mCols){
  253.     for(int i = 0 ;i < mCols; i++)
  254.         {
  255.             if(i % 2 == 0)
  256.             {
  257.                 for(int j = 0; j < nRows; j++)
  258.                 {
  259.                     cout << input[j][i] << " ";
  260.                 }
  261.             }
  262.             else
  263.             {
  264.                 for(int j = nRows - 1; j >= 0; j--)
  265.                 {
  266.                     cout << input[j][i] << " ";
  267.                 }
  268.             }
  269.         }
  270. }
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285. #include <iostream>
  286. using namespace std;
  287.  
  288. void spiralPrint(int **input, int nRows, int nCols){
  289.     int i, k = 0, l = 0;
  290.     while (k < nRows && l < nCols) {
  291.         for (i = l; i < nCols; i++) {
  292.             cout << input[k][i] << " ";
  293.         }
  294.         k++;
  295.  
  296.         for (i = k; i < nRows; i++) {
  297.             cout << input[i][nCols - 1] << " ";
  298.         }
  299.         nCols--;
  300.  
  301.         if (k < nRows){
  302.             for (i = nCols - 1; i >= l; i--) {
  303.                 cout << input[nRows - 1][i] << " ";
  304.             }
  305.             nRows--;
  306.         }
  307.  
  308.         if (l < nCols){
  309.             for (i = nRows - 1; i >= k; i--) {
  310.                 cout << input[i][l] << " ";
  311.             }
  312.             l++;
  313.         }
  314.     }
  315. }
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332. bool isPermutation(char input1[], char input2[]) {
  333.     int freq[256] = {0};
  334.     for(int i = 0; input1[i] != '\0'; i++) {
  335.         int index = input1[i];
  336.         freq[index]++;
  337.     }
  338.     for(int i = 0; input2[i] != '\0'; i++) {
  339.         int index = input2[i];
  340.         freq[index]--;
  341.     }
  342.     for(int i = 0; i < 256; i++) {
  343.         if(freq[i] != 0) {
  344.             return false;
  345.         }
  346.     }
  347.     return true;
  348. }
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368. void removeConsecutiveDuplicates(char input[]) {
  369.     int nextIndex = 1;
  370.     char lastChar = input[0];
  371.     for(int i = 0; input[i] != '\0';) {
  372.         if(input[i] == lastChar) {
  373.             i++;
  374.         }
  375.         else {
  376.             input[nextIndex] = input[i];
  377.             nextIndex++;
  378.             lastChar = input[i];
  379.             i++;
  380.         }
  381.     }
  382.     input[nextIndex] = '\0';
  383. }
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. void reverse(char input[], int start, int end){
  401.     int i = start, j = end;
  402.     while(i < j){
  403.         char temp = input[i];
  404.         input[i] = input[j];
  405.         input[j] = temp;
  406.         i++;
  407.         j--;
  408.     }
  409. }
  410. void reverseEachWord(char input[]){
  411.     int start = 0, end = 0, i;
  412.     for(i = 0; input[i] != '\0'; i++){
  413.         if(input[i] == ' '){
  414.             end = i - 1;
  415.             reverse(input, start, end);
  416.             start = i + 1;
  417.         }
  418.     }
  419.     end = i - 1;
  420.     reverse(input, start, end);
  421. }
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433. #include<cstring>
  434. void removeAllOccurrencesOfChar(char input[], char c) {
  435.     // Write your code here
  436.     int j, n = strlen(input);
  437.     for (int i=j=0; i<n; i++)
  438.        if (input[i] != c)
  439.           input[j++] = input[i];
  440.      
  441.     input[j] = '\0';
  442. }
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458. #include<bits/stdc++.h>
  459. #define ASCII_SIZE 256
  460. // input - given string
  461. char highestOccurringChar(char str[]) {
  462.     char result;
  463.     int i, len;
  464.     int max = -1;
  465.    
  466.     int freq[256] = {0};
  467.  
  468.     len = strlen(str);
  469.    
  470.     for(i = 0; i < len; i++)
  471.     {
  472.         freq[str[i]]++;
  473.     }
  474.        
  475.     for(i = 0; i < len; i++)
  476.     {
  477.         if(max < freq[str[i]])
  478.         {
  479.             max = freq[str[i]];
  480.             result = str[i];
  481.         }
  482.     }
  483.     return result;
  484. }
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500. string getCompressedString(string &input) {
  501. int count = 1;
  502.     char currentChar = input[0];
  503.     int i, nextIndex = 1;
  504.     for(i = 1; input[i] != '\0'; i++){
  505.         // Count the occurrence of consecutive duplicate character
  506.         if(input[i] == currentChar){
  507.             count++;
  508.         }
  509.         else{
  510.             // Append the count with string
  511.             if(count > 1){
  512.                 input[nextIndex] = char(count + 48);
  513.                 nextIndex++;
  514.             }
  515.             currentChar = input[i];
  516.             input[nextIndex] = input[i];
  517.             nextIndex++; count = 1;
  518.         }
  519.     }
  520.     if(count > 1){
  521.         input[nextIndex] = char(count + 48);
  522.         nextIndex++;
  523.     }input[nextIndex] = '\0';
  524. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement