Question

In: Computer Science

I am trying to tokenize a string using a function by passing the char string[] and...

I am trying to tokenize a string using a function by passing the char string[] and char *pointer[100]. While I have working code inside the int main(), I am having trouble actually declaring the parameters for the function.

I know how to pass the char array (char string[]), but not how to pass the char pointer array (char *pointer[100]).

This is my code below:

int main() {

   // Declare variables
   char str[] = "this is a test only - just a test";       // char array
   char *ptr1[100], *ptr2[100];                           // Creates two pointers

   function1(str, *ptr1[100]);

   return 0;
}

void function1(char str[], char *ptr1[100]) {

   int i = 0;
       int count = 0;

       // Declaration of strtok: char *strtok(char *str, const char *delim)
       // It breaks *str into a series of tokens using *delim
       char *p = strtok (str, " ");

       // Stores each piece into the array
       while (p != NULL) {
           ptr1[i++] = p;
           p = strtok(NULL, " ");
           count++;
       }

       // Prints the array
       for (i = 0; i < count; ++i) {
           printf("%s\n", ptr1[i]);
       }
}

For my code, it works if I get rid of the function and simply place the code in my int main(). Other times, I manipulated the second parameter and it started to work. However, the return values from 'p' transformed into integers from the "ptr1[i++] = p" portion of the code.

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include <stdio.h>

#include <string.h>

void function1(char str[], char *ptr1[100]);

int main()

{

    // Declare variables

    char str[] = "this is a test only - just a test"; // char array

    char *ptr1[100], *ptr2[100];                      // Creates two pointers

    function1(str, ptr1);

    return 0;

}

void function1(char str[], char *ptr1[100])

{

    int i = 0;

    int count = 0;

    // Declaration of strtok: char *strtok(char *str, const char *delim)

    // It breaks *str into a series of tokens using *delim

    char *p = strtok(str, " ");

    // Stores each piece into the array

    while (p != NULL)

    {

        ptr1[i++] = p;

        p = strtok(NULL, " ");

        count++;

    }

    // Prints the array

    for (i = 0; i < count; ++i)

    {

        printf("%s\n", ptr1[i]);

    }

}


Related Solutions

I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary...
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary string and iteratively constructs a decimal value (val) and width of each binary pattern (separated by spaces), until a space or a null character ('\0') is encountered in the string. Once a space or a null character is found, this function should call the assembly code (decode_morse()) to obtain the corresponding ASCII value, for the current val and width, and place the ASCII value...
Hi! I wrote two function to get count cluster of char in a string , charFreq(),...
Hi! I wrote two function to get count cluster of char in a string , charFreq(), and an other one which iterate through a vector line by line looking for last element on each line, last(). The problem is, after appending all chars in a string and try to count clusters of values in that string, I get seg fault. I feel like my logic behind is ok, but I am not sure what I did wrong. Can someone help...
IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX...
IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX INPUT: B C D A OUTPUT: D C B A #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]) {         int MAX = 100000;         int i =0;         int k =0;         int j =0;         char array[MAX];         char split[] = " ,.-!?()0123456789";         int n = 0;         char second[MAX];         printf("Please enter in a String: ");...
Left shift. I am trying to left shift a string located in a text file. I...
Left shift. I am trying to left shift a string located in a text file. I am using c++ and the goal is to left shift by 1 character a string of length N, and output on stdout all of the performed shifts. Can someone tell me what I am doing wrong, and tell me what I can fix? i.e: text file contains: Hi there!. Output on stdout: Hi there!, i there!H, _there!Hi, there!Hi_,...., !Hi_there. #here "_" represent space. -------------------------------------------------------------------------------------------------------------------------...
I am running this code using C to count the words in char array, but somehow...
I am running this code using C to count the words in char array, but somehow it keeps counting the total characters + 1. Any solution to fix this problem? #include <stdio.h> int main() {    char input[1001];    int count =0;    fgets(input, 1001, stdin);    char *array = input;    while(*array != '\0')       {        if(*array == ' '){            array++;        }        else{        count++;        array++;...
I am trying to create a function in JAVA that takes in an ArrayList and sorts...
I am trying to create a function in JAVA that takes in an ArrayList and sorts the values by their distance in miles in increasing order. So the closest (or lowest) value would be first. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. The code for the main class is not necessary. I am only really asking for the...
I am building a tik tac toe board using python and the function I am using...
I am building a tik tac toe board using python and the function I am using is not working to check to see if the board is full. I'll include the code for the board and the function to check if it is full. X's and O's are being inserted and the function is suppose to stop running when is_full(board) returns True. ch is either x or o board = []    for i in range(3):     board.append([])     for j...
I am trying to plot a difficult function in MATLAB, however I need to understand the...
I am trying to plot a difficult function in MATLAB, however I need to understand the basics first. I am trying to plot n=0 for x=0:0.01:2pi n=n+1 y(n)=sin(x) end I beleive what this says, is that I want to plot sin(x) over a full period hence from o to 2pi, and I beleive the 0.01 is the incremenation along the x-axis. I am not sure what my n is doing Could smeone please graph this for me with the MATLAB...
This is Using MATLAB: I am trying to store the solution of this matrix because I...
This is Using MATLAB: I am trying to store the solution of this matrix because I want to do something with the result like find the norm of that answer however I am stuck and cannot seem to be able to. Help would be appreciated! --------------------------------------------- MATLAB CODE: close all clear clc A = [1 -1 2 -1; 2 -2 2 -3; 1 1 1 0; 1 -1 4 5]; b = [-8 -20 -2 4]'; x = gauss_elim(A,b) function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT