Thread: Need help with strings and arrays

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    19

    Need help with strings and arrays

    Need help with this:

    1. Write a function that takes in a string and returns an array which contains the index of all the spaces in the string.
    For example, "This is an example" should return "5 8 11", which are the numbers of the space characters.
    It should also return the total number of spaces.

    2. Write a function that takes in a string and a character matrix. The function fills in the character matrix so that each line of the character matrix is an individual word in the string.
    So "This is an example" should be sent to the character matrix as:

    This
    is
    an
    example

    Here is what I have come up with, but I am stuck.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    int spaceTest(char x[], int out[])
    {
        int count=0, i; //use count for total number of spaces
        for(i=0; i<15;i++)
        {
            if(x[i]=' ')
            {
                out[i]=i;
                count++;
            }
        }
        return count;
    }
    
    
    
    
    void fillCharMatrix(char x[], char matrix[][1])
    {
        int i, count=1;
        for(i=0;i<4;i++)
        {
            while(x[i] != ' ')
            {
                matrix[count][1]=matrix[count][1]+x[i];//really not sure how to do this part
            }
            count++;
        }
    }
    
    
    int main()
    {
        char x[100]="This is an example";
        int out[100];
        int total= spaceTest(x,out);
        printf("Index of all the spaces\n");
        printf("%d\n",out);
        printf("Total number of spaces is %d\n",total);
        char matrix[100][1];
        fillCharMatrix(x,matrix);
        printf("The character matrix is:\n");
        printf("%c\n", matrix);
        return 0;
    }
    So far the program runs fine, but nothing prints out correctly, but I just cant figure out what to change.
    Im really just stuck inside the two loops, please let me know how I can get the problem working correctly. Thanks for the help!

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    In spaceTest() you should have out[count]=i; instead of out[i]=i;
    In main you forgot to print out the 'out' array. I haven't looked at the 2nd part of the problem yet.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    This has an error:
    Code:
    if(x[i]=' ')
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Good catch! That's why I always put the constant literal first, as in if (' ' == x[i]) so that any similar errors of assignment get caught immediately.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    19
    Great, my number of spaces printed out right, but the index still isn't printing out "5 8 11" and the character matrix isn't working.

    In fact, now the program stalls after it prints out the number of spaces, can anyone help me with the issue?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I can't make heads or tails out of your code, above, but getting 5 8 11 is easy.

    Code:
    int x[100]={"This is an example"};
    int out[100]={0};
    int countOfSpaces=0;
    int i, j;
    
    for(i=0;x[i];i++) {
       if(x[i] == ' ')
          out[i] = 1;
    }
    
    for(int j=0;j<i;j++) {
       if(out[j]) {
           printf("%d ", j );
          countOfSpaces++;
       }
    }
    printf("\n\nThere were %d spaces",countOfSpaces);
    Study that logic, and see what you think.

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    19
    Ok, I got my first function working

    Now I have this for my second function
    Code:
    
    void fillCharMatrix(char x[], char matrix[][100])
    {
        int i, count=1;
        for(i=1;i<18;i++)
        {
            matrix[count][i]=x[i];
            if(x[i] == ' ')
            {
                count++;
            }
        }
    }
    What should I change here to correctly display the matrix as

    This
    is
    an
    example

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    if(x[i]=' ')
    = -> ==

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Walk through the letters in the string of char's. See if the letter you're looking at is a space. If it is, then print a newline char, instead of the space.

    Study this assignment long and hard, because I assure you that a lot of string problems are solved using this basic "walk through the letters, testing each one", technique.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays of strings
    By ceem in forum C Programming
    Replies: 1
    Last Post: 03-03-2011, 01:17 AM
  2. Arrays/strings
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 10-22-2008, 07:31 PM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. strings and arrays
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2002, 12:52 PM
  5. Strings And Arrays
    By helbovine in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 03:06 PM