Advertisement
apl-mhd

reverse2dStrinDemo

Oct 29th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4.     char str[50][50];
  5.  
  6. int wordCount(char name[50]){  /* count total word in a string*/
  7.  
  8.     int totalWord = 1, i=1;
  9.  
  10.  
  11.     while(name[i] != '\0'){
  12.  
  13.         if( name[i] == ' '){
  14.  
  15.             totalWord++;
  16.         }
  17.  
  18.         i++;
  19.     }
  20.  
  21.  
  22.     return totalWord;
  23.  
  24. }
  25.  
  26.  
  27.  
  28. void copyString(char name[50], int size, int totalWord){ /* copy string in 2d char array reverse*/
  29.  
  30.     int i, j=0, temp, m=0;
  31.  
  32.     for(i = 0; i < totalWord; i++){
  33.             m = 0;
  34.         while(name[j] != '\0'){
  35.  
  36.                 str[i][m] =name[j];
  37.                 printf(" %d \n", j);
  38.                 j++;
  39.                 m++;
  40.  
  41.                 if(name[j-1] == ' ')
  42.                     break;
  43.         }
  44.  
  45.  
  46.     }
  47.  
  48.     printf("%s", str[0]);
  49.     printf("%s", str[1]);
  50.     printf("%s", str[2]);
  51.     printf("%s\n", str[3]);
  52.         printf("-------------------------------------------------\n");
  53.  
  54.  
  55.    // printf(" %c %c %c %c ", str[0][0], str[0][1], str[0][2], str[0][3]);
  56.  
  57. }
  58.  
  59.  
  60. /*********************** reverse *****************************************/
  61.  
  62. void  stringReverse(char twodString[50][50], int totalWord){
  63.  
  64.     int i , j, m, n ;
  65.     char temp;
  66.  
  67.  
  68.  
  69.     for(i = 0; i < totalWord; i++){
  70.  
  71.        n = strlen(twodString[i]) -1;
  72.        m = 0;
  73.  
  74.         while(m < n){
  75.  
  76.            temp = twodString[i][m];
  77.            twodString[i][m] = twodString[i][n];
  78.            twodString[i][n] = temp;
  79.  
  80.             m++;
  81.             n--;
  82.            }
  83.  
  84.         }
  85.  
  86.  
  87.     printf("%s", str[0]);
  88.     printf("%s", str[1]);
  89.     printf("%s", str[2]);
  90.     printf("%s\n", str[3]);
  91.     printf("-------------------------------------------------\n");
  92.  
  93.  
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. int main(int argc, char *argv[])
  103. {
  104.  
  105.  
  106.  
  107.  
  108.     char name[80] = "NUR, REHTOM! REDRUM!!";
  109.  
  110.     int stringLength, totalWord;
  111.  
  112.     stringLength = strlen(name);
  113.  
  114.    totalWord = wordCount(name);
  115.  
  116.    printf("total %d \n", totalWord);
  117.  
  118.    copyString(name, stringLength, totalWord);
  119.  
  120.    stringReverse(str, totalWord);
  121.  
  122.  
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement