Question

you may not use any of the C-string library functions such as strlen(), strcpy(), strstr(), etc....

you may not use any of the C-string library functions such as strlen(), strcpy(), strstr(), etc. You must write your own. You might consider writing helper functions to do tasks that many of these functions require, e.g. finding the last character of the string, and then use those helper functions where convenient.

This function finds all instances of the char ‘target’ in the string and replaces them with ‘replacementChar’. It also returns the number of replacements that it makes. If the target char does not appear in the string it returns 0 and does not change the string. For example, if s is “go giants”, target is ‘g’, and replacement is ‘G’, the function should change s to “Go Giants” and return 2.

int replace(char *s, char target, char replacementChar)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

int str_length(char* s)
{
int count = 0;
while (*s) {
s++;
count++;
}
return count;
}

int replace(char* s, char target, char replacementChar)
{
int len = str_length(s);
int i, count = 0;
for (i = 0; i < len; ++i) {
if (s[i] == target) {
++count;
s[i] = replacementChar;
}
}
return count;
}

int main()
{
char str[100], target, replacement;
int count;
printf("Enter a string: ");
scanf("%s", str);
printf("Enter target character: ");
scanf(" %c", &target);
printf("Enter replacement character: ");
scanf(" %c", &replacement);
count = replace(str, target, replacement);
printf("modified string is %s and number of occurances of %c were %d\n", str, target, count);
return 0;
}

\color{red}\underline{Output:}

Add a comment
Know the answer?
Add Answer to:
you may not use any of the C-string library functions such as strlen(), strcpy(), strstr(), etc....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ please! *Do not use any other library functions(like strlen) than the one posted(<cstddef>) in the code below!* /** CS 150 C-Strings Follow the instructions on your handout to complete t...

    In C++ please! *Do not use any other library functions(like strlen) than the one posted(<cstddef>) in the code below!* /** CS 150 C-Strings Follow the instructions on your handout to complete the requested function. You may not use ANY library functions or include any headers, except for <cstddef> for size_t. */ #include <cstddef> // size_t for sizes and indexes ///////////////// WRITE YOUR FUNCTION BELOW THIS LINE /////////////////////// ///////////////// WRITE YOUR FUNCTION ABOVE THIS LINE /////////////////////// // These are OK after...

  • In C Programming Language In this lab you will implement 4 string functions, two using array...

    In C Programming Language In this lab you will implement 4 string functions, two using array notation and two using pointers. The functions must have the signatures given below. You may not use any C library string functions. The functions are 1. int my strlen (char s ) - This function returns the number of characters in a string. You should use array notation for this function. 2. int my strcpy (char s [], char t I)- This function overwrites...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • Cannot use/call any library functions. Write a C function char* my_strcat(char *dest, const char *src) that...

    Cannot use/call any library functions. Write a C function char* my_strcat(char *dest, const char *src) that copies the contest of string src to the end of string dest, assuming that there is enough storage space in dest to accommodate the resulting concatenated string. The function returns dest.

  • Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified...

    Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified character in a string) Write a recursive function that finds the number of occurrences of a specified letter in a string using the following function header. int count(const string& s, char a) For example, count("Welcome", 'e') returns 2. Write a test program that prompts the user to enter a string and a character, and displays the number of occurrences for the character in the...

  • using basic c++ and use no functions from c string library b) Write a fragment of...

    using basic c++ and use no functions from c string library b) Write a fragment of code (with declarations) that reads a pair of words from the keyboard and determines how many letters in corresponding positions of each word are the same. The words consist only of the letters of the English alphabet (uppercase and lowercase). For example, if the words are coat and CATTLE, the following output should be generated: 012245 0122 matching letter in position 0 t is...

  • This program should be run on Visual Studio. Please use printf and scanf as input and output. Tha...

    This program should be run on Visual Studio. Please use printf and scanf as input and output. Thank you 6.12 Lab Exercise Ch.6b: C-string functions Create and debug this program in Visual Studio. Name your code Source.c and upload for testing by zyLabs You will write 2 functions which resemble functions in the cstring library. But they will be your own versions 1. int cstrcat(char dstDchar src) which concatenates the char array srcl to char array dstD, and returns the...

  • I USE NETBEANS. C++ PLEASE. COULD YOU PLEASE WRITE A CODE THAT ACTUALLY WORKS, PLEASE PLEASE PLEA...

    I USE NETBEANS. C++ PLEASE. COULD YOU PLEASE WRITE A CODE THAT ACTUALLY WORKS, PLEASE PLEASE PLEASE EXPLAIN WHAT EVERYTHING IS DOING. INCLUDE SCREENSHOTS OF OUTPUTS PLEASE. I REALLY APPRECIATE THE HELP. THANK YOU <3 100% RATING IF CORRECT AND IS UNDERSTANDABLE. PLEASE ONLY CODES THAT WORK AND DO THE TASK AT HAND. 2 Write code for these functions using the string API: A boolean function no_el1 that takes a string as parameter and returns true if the string does...

  • You will implement some helpful string functions. The prototypes are below. The only built-in functions you...

    You will implement some helpful string functions. The prototypes are below. The only built-in functions you can use are toupper() and tolower() – found in ctype.h. char* capitalize(char* s); char* str2Lwr(char* s); char* str2Upr(char* s); All of the functions should be "in-place". In-place means that you should update the string pointed to by s, rather than creating a new one. capitalize() should convert the first character of string s to upper case. The value of s is returned. str2Lwr() should...

  • 3. (24%) Write the following functions. You MUST use recursion for credit. You may use the...

    3. (24%) Write the following functions. You MUST use recursion for credit. You may use the string library functions: indexing, lengthO, +, and substr(int n) (recall that substr(n) returns a string with all but the first n characters). (a) string alternate(string s): returns a string that contains every other element of s starting at the beginning. For example, alternate( 'dog' ') ''dg'' (b) string uc(string s): returns a string that is the same as s except that every upper case...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT