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

If pointer 1 is spun and then pointer 2 is ​spun, determine the probability of the...
If pointer 1 is spun and then pointer 2 is ​spun, determine the probability of the pointers landing on a color other than yellow on the first spin and a color other than red on the second spin. Pointer 1      Pointer 2 pointer 1 has 2 sections 50% red and 50% yellow.. pointer 2 has 3 sections 50% blue and 25% red and 25% yellow
Create a function that will return true if numbers X and Y exist within S such that X + Y = Z, return false otherwise.
Given:   S = [ 2, 1, 3, 4, 7, 5 ] Z = 8   Create a function that will return true if numbers X and Y exist within S such that X + Y = Z, return false otherwise.   Code in Java 8. 
Using pseudocode or C++ code, write a function to compute and return the product of two...
Using pseudocode or C++ code, write a function to compute and return the product of two sums. The first is the sum of the elements of the first half of an array A. The second is the sum of the elements of the second half of A. The function receives A and N ≥ 2, the size of A, as parameters. (in c++)
C# windows application form. Create a base class to store characteristics about a loan. Include customer...
C# windows application form. Create a base class to store characteristics about a loan. Include customer details in the Loan base class such as name, loan number, and amount of loan. Define subclasses of auto loan and home loan. Include unique characteristics in the derived classes. For example you might include details about the specific auto in the auto loan class and details about the home in the home loan class. Create a presentation class to test your design by...
Write a C function to add the elements of two same-sized integer arrays and return ptr...
Write a C function to add the elements of two same-sized integer arrays and return ptr to a third array. int *addTwoArrays(int *a1, int *b1, int size); The function should follow the following rules: If the sum for any element is negative, make it zero. If a1 and b1 point to the same array, it returns a NULL If any input array is NULL, it returns a NULL. Please call this function with the following arrays and print the sums...
Create an integer to std_logic_vector function(i2std)
Create an integer to std_logic_vector function(i2std)
As an administrative professional, create a paper on the skills and procedures and include:
As an administrative professional, create a paper on the skills and procedures and include:Identifying techniques for managing time, stress, and anger in the workplaceApplying conflict resolution skills in the workplaceDescribe strategies and tools for managing your workloadExplain steps for setting and meeting goals and establishing priorities in the workplaceDefine the steps necessary for ethical change in the workplaceIdentify traits of an ethical administrative professionalList and describe basic workplace standards as found in the Canada Labour CodeIdentify characteristics of ethical businesses...
(a) What is a pointer? (b) What is a dereferencing operator?
 (a) What is a pointer? (b) What is a dereferencing operator? (c) What is the difference between assignment statements p1 = p2; and *p1 = *p2; (d) What is a dynamic variable? (e) What is the purpose of the new operator? (f) What is the purpose of the delete operator? (g) What is the freestore (also called the heap)? (h) What is the difference between dynamic variables and automatic variables?
Create two series of return at 5 periods in length. Call them Return Series A and...
Create two series of return at 5 periods in length. Call them Return Series A and Return Series B. These two series of returns are independent of one another and that you are a risk-averse investor, explain which series of returns is preferable. Now, create two series of costs at 5 periods in length. Call them Cost Series C and Cost Series D. These two series of costs are independent of one another and that you hold and aversion to...
Generate a scatter plot of this data, and include a smoothed function using smooth.spline function in...
Generate a scatter plot of this data, and include a smoothed function using smooth.spline function in R. Use predict function in R to calculate the 95% bounds (confidence band) for the mean, and plot them on the same scatterplot (use lty=2, and col=3). How would I go about writing this code in R?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT