user3501848
user3501848

Reputation: 21

Getting the last word from a string

In a assignment I need to be able to find the last word from a two dimensional array of strings (the last word for each line/string) so I can apply a cipher to it. I have tried this:

space = strrchr((*array)[i], ' ');                                                 
if ((name = strstr((*array)[i], search)) != NULL) {
    cipher = 1;
    }
if (cipher && name > space) {    
 //Cipher code here

Space and Search are defined as:

char *space, *search;

*array is a pointer to a two-dimensional array that is declared in main() and I load values into it in a different function.

The code works, but the program crashes after pulling the last word of the first line. While debugging, I get the error: Unhandled exception at 0x009915B0 in Projekt 1.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC. The word from the first line is pulled correctly, and the cipher works (I have tried to print it after each line).

I have tried Googling and consulted other topics on StackOverflow but it didn't help me much. Could anyone point me in the right direction on how to get the last word for each line?

EDIT: I am using the Albam cipher in this case:

for (i = 0; i < lines; i++) {

space = strrchr((*array)[i], ' ');                                                 
if ((name = strstr((*array)[i], search)) != NULL) {
    cipher = 1;
    }

if (cipher && name > space) {                                                 

    cipher = 1;
    for (j = 0; j < strlen(*array[i]); j++) {

        if ((*array)[i][j] < 'Z' && (*array)[i][j] > 'A' || (*array)[i][j] < 'z' && (*array)[i][j] > 'a') { 
          if (toupper((*array)[i][j]) > 'A' && toupper((*array)[i][j]) < 'N')
              (*array)[i][j] = (*array)[i][j] + 13;
          if (toupper((*array)[i][j]) > 'N' && toupper((*array)[i][j]) < 'Z')
              (*array)[i][j] = (*array)[i][j] - 13;
        }
      }
    }
}

Upvotes: 1

Views: 2376

Answers (1)

abhilb
abhilb

Reputation: 5757

Hope this helps

code of interest

for(i=0; i<rows; i++)
    {
        for(j=0; j<cols; j++)
        {

            lastWord = strrchr(array[i][j], ' ');
            if (lastWord == NULL)
                lastWord = array[i][j];
            else
                lastWord++;
            printf("%s\n", lastWord);
            lastWord = NULL;

        }            
    }      

Full code

int i,j;
char *line = NULL;
int read, len;
char*** array;
char* lastWord;
int rows, cols;

FILE* fp;
fp = fopen("input.txt", "r");


if(fp != NULL)
{
    // Read the number of rows and cols
    line = NULL;
    read = getline(&line, &len, fp);
    sscanf(line, "%d,%d", &rows, &cols);

    // allocate memory and read the input strings
    array = (char***)malloc(sizeof(char**) * rows);
    for(i=0; i<rows; i++)
    {
        array[i] = (char**)malloc(sizeof(char**) * cols);
    }

    for(i=0; i<rows; i++)
    {
        for(j=0; j<cols; j++)
        {
            read = getline(&line, &len, fp);
            assert((read >= 0) && (read <= 1024) && (line != NULL));

            array[i][j] = (char*)malloc(read+1);
            strncpy(array[i][j], line, read-1);   
        }
    }

    if(line != NULL)                
    {
        free(line);
        line = NULL;
    }

    for(i=0; i<rows; i++)
    {
        for(j=0; j<cols; j++)
        {

            lastWord = strrchr(array[i][j], ' ');
            if (lastWord == NULL)
                lastWord = array[i][j];
            else
                lastWord++;
            printf("%s\n", lastWord);
            lastWord = NULL;

        }            
    }            


    for(i=0; i<rows; i++)     
        for(j=0; j<cols; j++)
            free(array[i][j]);
    for(i=0; i<rows; i++)
        free(array[i]);
    free(array);       
}
else
{
    printf("Unable to open the file\n");
}

fclose(fp);    

return 0;

Upvotes: 1

Related Questions