ON MY LAST NERVE

dpmunky
dpmunky used Ask the Experts™
on
i am trying to take a single dimensional array and place it into a 2 dimensional array and print the whole thing so it looks like this...

Total Words: 6

Word Count     Word               Character Count
-------------------------------------------------------
1               SEA                         3
2               TO                         2
3               SHINING                    7
4               C                         1
5               HELLO                    5
6               WORLD                    5
--------------------------------------------------*/

for whatever reason only the 1st word prints???..im so frustrated with this thing right now...i hate asking for help but i cant help it

heres my source for the project

***********************
#include <stdio.h>


int Capitalize(int ch);
void UserInput(char string[]);
void NEW_2D_array(char* string);

char TWO_D_array[1024][100];
int wordcount, charcount;
int r, c, i;


int main()
{
     char str[1024];
     int i, ch;
     ch = 0;
     

     printf("Enter a string of characters\n");        /* this is where the user */
     printf("End input by pressing <CTRL-Z>\n\n");      /* will input their string */
     printf("Enter string: ");
     UserInput(str);

     /* this part goes through every letter of the */
     /* array and checks to see if the letter is upper case */
     for (i = 0; str[i] != '\0'; i++)    
     {                                            
          str[i] = (char) Capitalize(str[i]);
     }

     NEW_2D_array(str);

     printf("Word Count           Word          Character Count\n");
     printf("--------------------------------------------------\n");
     printf("%-21d%s%10d\n", wordcount, TWO_D_array, charcount );


     return 0;
}

void NEW_2D_array(char* string)
{
     r = c = i = 0;
     charcount = 0;
     wordcount = 1;

     for (i = 0; string[i] != '\0'; i++)
     {
          TWO_D_array[r][c] = string[i];

          if ((string[i] == ' ') || (string[i] == '\n'))
          {
               r++;
               wordcount++;
               c = 0;
          }

          else
          {
               c++;
               charcount++;
          }
         
     }
}


/* this function capitalizes letters that are lower case by using math */
int Capitalize(int ch)
{
     if (( 'a' <= ch) && (ch <= 'z' ))
     {
          return (ch + 'A' - 'a');
     }

     else
     {
          return ch;
     }
}


/* takes input from user by cycling through every letter in the string up until
** the EOF marker, when the marker is hit, the last element in the array is set
** as the NULL character */
void UserInput(char string[])
{
     int ch;
     int i = 0;
     
     while ( (ch = getchar()) != EOF )
     {          
          if (ch != '\n')
          {
               string[i++] = (char) ch;
          }
     }

     string[i] = '\0';

}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2014
Top Expert 2015
Commented:
#include <stdio.h>


int Capitalize(int ch);
void UserInput(char string[]);
void NEW_2D_array(char* string);

char TWO_D_array[1024][100];
int wordcount[1024], charcount[1024];
int r, c, i;


int main()
{
    char str[1024];
    int i, ch;
    ch = 0;
   

    printf("Enter a string of characters\n");        /* this is where the user */
    printf("End input by pressing <CTRL-Z>\n\n");      /* will input their string */
    printf("Enter string: ");
    UserInput(str);

    /* this part goes through every letter of the */
    /* array and checks to see if the letter is upper case */
    for (i = 0; str[i] != '\0'; i++)    
    {                              
         str[i] = (char) Capitalize(str[i]);
    }

    NEW_2D_array(str);

    printf("Word Count           Word          Character Count\n");
    printf("--------------------------------------------------\n");
    for( i=0; i<=r; i++ ){
         printf("%-21d%s%10d\n", i+1, TWO_D_array[i], charcount[i] );
    }

    return 0;
}

void NEW_2D_array(char* string)
{
    r = c = i = 0;
    charcount[r] = 0;
    wordcount[r] = 1;

    for (i = 0; string[i] != '\0'; i++)
    {
         TWO_D_array[r][c] = string[i];

         if ((string[i] == ' ') || (string[i] == '\n'))
         {
               r++;
               wordcount[r]=r+1;
               charcount[r]=0;
               c = 0;
         }
         else
         {
               c++;
               charcount[r]++;
         }
         
    }
}


/* this function capitalizes letters that are lower case by using math */
int Capitalize(int ch)
{
    if (( 'a' <= ch) && (ch <= 'z' ))
    {
         return (ch + 'A' - 'a');
    }

    else
    {
         return ch;
    }
}


/* takes input from user by cycling through every letter in the string up until
** the EOF marker, when the marker is hit, the last element in the array is set
** as the NULL character */
void UserInput(char string[])
{
    int ch;
    int i = 0;
   
    while ( (ch = getchar()) != EOF )
    {          
         if (ch != '\n')
         {
              string[i++] = (char) ch;
         }
    }

    string[i] = '\0';

}

Author

Commented:
im just sorry i dont have more points... BIG HELP!!!

i guess it just goes to show 2 or 3 lines can mess up everything

again dude..THANKS!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial