0

I am currently working on a small project but have come to a bit of a standstill. At present I have a code that makes two arrays; one containing a series of integers and the other a series of characters. For example:

arrayI = [5, 5, 3]
arrayC = [hellotheresir]

What I want to do, given then integers in the arrayI, is make "words" (as in strings) out of arrayC. To do this I want to take the integer and say given the integer take that many characters out of arrayC, and turn them into a string. Then, continuing from this same point in the arrayC, go to the next integer and do the same thing until both arrays are exhausted. I can also confirm that the integers will always equate to the right number of letters to make the words. In the above example:

First the 5 is taken and checked, and makes a string "hello" (stored somewhere else until done) Then the second 5 is taken from the point the last one left off, and the string "there" is created. Then finally the 3 is taken and the string "sir" is created.

Essentially the integer defines the len of the string to be created.

My code thus far:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 
char** wordList(const char* s) { 
    char key[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
int arr[1000]; 
char *arry[1000];
int counter = 0;
char * pch; 
int pch2; 
int count = 0; 
pch = strpbrk (s, key); 
pch2 = strspn(s, key); 
for (int i = 0; i < strlen(s); i++) { 
    pch2 = strspn(s+i, key); 
if ((strspn((s+i)-1, key) == 0) && (pch2 != 0)) { 
    arr[count] = pch2; 
count ++; 
} 
} 
while (pch != NULL) { 
    arry[counter] = pch; 
pch = strpbrk (pch+1, key); 
counter ++; 
} 

return 0; 
} 




int main (void) { 
    char** words = wordList("Gadzooks!', he cried."); 
}

For reference, the code is simply trying to make an array of words as strings, but I currently only require help with the two arrays part.

3
  • 1
    Where does this integer sequence you wrote of in your description appear in your code? And what is the actual question ? Commented Aug 30, 2015 at 1:39
  • Hi sorry, to clarify the array arr is the integer array, while the array arry is the character array. Commented Aug 30, 2015 at 1:41
  • You description suggests you want to use the integer array to create strings from the input array (which should be trivial). But your code seems to be trying to build the integer array; it is used only once, and that by assignment, never by eval. Are you sure this is the correct code for the question you asked? Commented Aug 30, 2015 at 1:43

2 Answers 2

1

I really didn't understand what you trying to do , but just for you to know the way you put this "strings" -> "Gadzooks!', he cried." will save the string on the static storage and you will not be able to do most of the function on them , you better work with strdup in your function or malloc and calloc to be able to use all the function . any way if you will explain simpler what you want to do I could help more .

Sign up to request clarification or add additional context in comments.

1 Comment

Mainly the overall point of the entire code is to return the strings in the format mentioned above, but without any of the punctuation in it. So essentially the punctuation acts as a break point for each word, so that the code knows when a new word needs to be created. So mainly what I am trying to do is using the integers and character array, make strings (or something more practical) and put them in a sort of array that can store these completed words so I can print them out later on. For example stringArray[0] would contain the first of the words.
0

Perhaps, such as(Error handling is omitted):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char **extraction(size_t size, int arrayI[size], const char arrayC[]){
    char **words = malloc(size * sizeof(*words));
    const char *p = arrayC;
    for(size_t i = 0; i < size; ++i){
        words[i] = malloc(arrayI[i] + 1);
        memcpy(words[i], p, arrayI[i]);
        p += arrayI[i];
        words[i][arrayI[i]] = 0;
    }
    return words;
}

int main(void){
    int arrayI[] = {5, 5, 3};
    char arrayC[] = {"hellotheresir"};

    size_t size = sizeof(arrayI)/sizeof(*arrayI);
    char **words = extraction(size, arrayI, arrayC);
    for(size_t i=0; i<size; ++i){
        puts(words[i]);
        free(words[i]);
    }
    free(words);

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.