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....
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]...
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 +...
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
8.20 Person Pointer Function Make a function that will accept a pointer to a vector of...
8.20 Person Pointer Function Make a function that will accept a pointer to a vector of Person pointers as a parameter. Return a pointer to the Person with the highest salary. The function must be signatured like: Person* getBestPaid(vector*); The class definition is: class Person { public: double salary; string name; }; You may include code in your main() to test, but the tests in this assignment will ensure your code is correct. You may assume there will ways be...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I have random array [4,2,7,1,9,8,0]. Sort the array's index but cannot change the position of item in the array, if you copy the array to a new array, you also cannot change the item's position in array. The index now is[0,1,2,3,4,5,6] The output should be[6,3,1,0,2,5,4]
In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
construct c program flow chart #include <stdio.h> #include <math.h> #include <string.h> #define num 6 #define b...
construct c program flow chart #include <stdio.h> #include <math.h> #include <string.h> #define num 6 #define b 6 #define t 6 double bmical(double w, double h){ double o; double bmi; o = pow(h,2); bmi = w/o; return bmi; } double maxheartrate(int num1, int age){ double mhr; mhr = num1 - age; return mhr; } double heartratereserve(double mhr, double rhr){ double hrr; hrr = mhr - rhr; return hrr; } double minrange(int hrr, int rhr){ double mirt; mirt = (hrr * 0.7)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT