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!