Question

In: Computer Science

Create C function for String.c and Strings.h that #include <string.h> /* substring - return a pointer...

Create C function for String.c and Strings.h that

#include <string.h>

/* substring - return a pointer to the substring beginning at the iPos-th position.
* If iPos is out-of-range, return (char*)NULL
*/
char* substring(char* str, int iPos);

/* charPosition - return the position of the first occurance of c in str.
* If c not present, return -1
*/
int charPosition(char* str, char c);

Solutions

Expert Solution

required functions

char* substring(char* str, int iPos)
{
    int i=0;
    while(str[i]!='\0')  /* string is terminated with '\0' in C. traverse string */
    {
        if(i == iPos)   /* if index is same as position passed to function then return address of that charcter*/
            return &str[i];
        
        i++;
    }
    return (char*)NULL;  /* if control comes out of the loop that means position exceeds length of string so 
    return NULL */
}

int charPosition(char* str, char c)
{
    int i=0;
    while(str[i]!='\0')  /* traverse string until '\0' is found */
    {
        if(str[i] == c)   /*if character at position i is equal to character to be found then return position*/
            return i;
            
        i++;
    }
    return -1;  /* if control comes out of loop that means character not found then return -1*/
}

complete program with driver code

#include <stdio.h>

char* substring(char* str, int iPos)
{
    int i=0;
    while(str[i]!='\0')  /* string is terminated with '\0' in C. traverse string */
    {
        if(i == iPos)   /* if index is same as position passed to function then return address of that charcter*/
            return &str[i];
        
        i++;
    }
    return (char*)NULL;  /* if control comes out of the loop that means position exceeds length of string so 
    return NULL */
}

int charPosition(char* str, char c)
{
    int i=0;
    while(str[i]!='\0')  /* traverse string until '\0' is found */
    {
        if(str[i] == c)   /*if character at position i is equal to character to be found then return position*/
            return i;
            
        i++;
    }
    return -1;  /* if control comes out of loop that means character not found then return -1*/
}

int main()
{
    
    char str[] ="Hello World";
    char *str1=substring(str,6);
    if(str1 != NULL)
        printf("\n%s",str1);
    else
        printf("\nposition is out of string length");
        
    str1 = substring(str,45);
    if(str1 != NULL)
        printf("\n%s",str1);
    else
        printf("\nposition is out of string length");
    
    int pos = charPosition(str,'l');
    if(pos==-1)
    {
        printf("\ncharacter not found");
    }
    else
    {
        printf("\n Character found at position %d ",pos);
    }
    
    pos = charPosition(str,'z');
    if(pos==-1)
    {
        printf("\ncharacter not found");
    }
    else
    {
        printf("\n Character found at position %d ",pos);
    }
    return 0;
}

output screenshot


Related Solutions

Use the given Strings.c and Strings.h module: Strings.c: #include "Strings.h" #include <string.h> #include<stdlib.h> #include<stdio.h> char* substring(char*...
Use the given Strings.c and Strings.h module: Strings.c: #include "Strings.h" #include <string.h> #include<stdlib.h> #include<stdio.h> char* substring(char* str, int iPos){ if(iPos > strlen(str)||iPos < 0)return (char*)NULL; char* substr; substr = &str[iPos]; return substr; } int charPosition(char* str, char c){ char* string = (char*)malloc(strlen(str)+1); int i; for(i = 0; i < strlen(str)+1; i++) { if(str[i] == c) { return i; } } return -1; } Strings.h: #include <string.h> /* substring - return a pointer to the substring beginning at the iPos-th position....
Create a function output() in C that takes the pointer to the array and its size...
Create a function output() in C that takes the pointer to the array and its size and prints the arrays' contests. (function prototype : void output(int *arrayPtr, int size);
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate number non white space characters int GetNumOfNonWSCharacters(string str) { int i = 0; int count = 0; while(str[i] != '\0') { if(str[i] != ' ') { count += 1; } i++; } return count; } // function to calculate numbers of words int GetNumOfWords(string str) { int i = 0; int count = 1; while(str[i] != '\0') { if(str[i] == ' ' && str[i-1]...
Please write program in c++ with using pointer. create a function that can find the number...
Please write program in c++ with using pointer. create a function that can find the number of even numbers that are between the maximum and minimum elements of an given array.The program have to use pointer. example 8 -1 2 2 1 9 2 4 0 output: 2
write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
** in C language please Write a function that remove a substring of str beginning at...
** in C language please Write a function that remove a substring of str beginning at index idx and length len. // param str: the string being altered // param idx: the starting index of the removed chunk // param len: the number of characters to remove (length of sub> // removed) assumptions: // 0 <= idx // str is a valid string that terminates properly // // TODO: complete the function void remove_substr(char str[], int idx, int len) {
Send an element from 2d array to a function using pointer to pointer. c++ I am...
Send an element from 2d array to a function using pointer to pointer. c++ I am having an issue with being able to send specific elements to the swap function. #include <iostream> using namespace std; void swap(int **a, int **b){    int temp = **a;    **a=**b;    **b=temp; } void selSort(int **arr, int d){    for(int i =0; i<d-1; i++){        int min = *(*arr+i);        int minin = i;        for(int j =i+1; j<d; j++){...
C++ Write a toVector function that takes in a C-style pointer of any type and a...
C++ Write a toVector function that takes in a C-style pointer of any type and a size (int) and returns a vector of the same size containing a copy of the elements in the input array. You may assume that the array elements have a valid copy constructor. Please explain every line of code to me
C++ Write a toVector function that takes in a C-style pointer of any type and a...
C++ Write a toVector function that takes in a C-style pointer of any type and a size (int) and returns a vector of the same size containing a copy of the elements in the input array using a template. You may assume that the array elements have a valid copy constructor. Please explain every line of code to me
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT