Advertisement
apl-mhd

reverseStringTwoDaRRayFinal

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