Question

IN C++ Create a method called xChar that will replace a specified char in a String...

IN C++
Create a method called xChar that will replace a specified char in a String with the 'X' character. Your function must use a pointer to search and replace the char. You are not allowed to use array notation to solve this problem.
Your function should take as arguments in the CString to search through and a char variable that holds the char to be searched for and replaced. The function should also return the number of replacements made.

Example:

Enter some text

Hello Word

Enter a character to x

o

here is your output

Hellx Wxrd

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

/*
 *  C++ Program to replace a character in a string
 */

#include <iostream>
using namespace std;

#define MAX 30

int xChar(char *str, char x)
{
  int count = 0;

  for (int i = 0; (*(str + i)) != '\0'; i++)
  {
    if (*(str + i) == x)
    {
      *(str + i) = 'x';
      count++;
    }
  }
  
  return count;
}

int main()
{
  char text[MAX], X;

  cout  << "Enter some text " << endl;
  cin.getline(text, MAX);

  cout << "Enter a character to x " << endl;
  cin >> X;

  xChar(text, X);

  cout << "here is your output" << endl;
  cout << text;

  return 0;
}

/*  Program ends here */

Add a comment
Know the answer?
Add Answer to:
IN C++ Create a method called xChar that will replace a specified char in a String...
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
  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • #include <iostream>   #include <string>   using namespace std;      void get_user_string(string *);   string convert_to_dash(String* );   int_search_and_replace(char, string,...

    #include <iostream>   #include <string>   using namespace std;      void get_user_string(string *);   string convert_to_dash(String* );   int_search_and_replace(char, string, string &);       int main (){    string s;    cout << "Enter a string:" << endl;    get_user_string(&s);        string dash_version = convert_to_dash(&s)    if ( dash_version != 32){    &s.push_back('-');    } Here is an example operation of the completed program: Please enter a string: the weather is great! The dash-version of your string is: Please tell me the char that...

  • Our lab today revolves around Character Strings (C-Strings) which are essentially character arrays. We’ll be writing...

    Our lab today revolves around Character Strings (C-Strings) which are essentially character arrays. We’ll be writing two functions that will output the number of vowels and consonants in a user inputted string. We’ll be using functions from the cstring library, so be sure to include it! We’ll be calling those functions method_one and method_two. To start off, declare a character array (with no size) called vowels and initialize it with “aeiouyAEIOUY” in our main function. Declare a character array with...

  • Write this function in c++.Use of string and <cstring> is not allowed .You are required to...

    Write this function in c++.Use of string and <cstring> is not allowed .You are required to use char* and implement the use of ** in this question . Gtest case to pass: #include "q1.cpp" #include <gtest/gtest.h> //-------------------Q1_8----------------- TEST(Question1_8, First) { char t1[]="Hello World"; char res1[] = "Hello"; char res2[] = "World"; char** r= StrTok(t1,' '); ASSERT_EQ(0, strcmp(r[0],res1) ); ASSERT_EQ(0, strcmp(r[1],res2) ); } TEST(Question1_8, Second) { char t1[]="Hello?World?OOP"; char res1[] = "Hello"; char res2[] = "World"; char res3[] = "OOP"; char**...

  • Note wordBank, an array of 10 strings (char *s). Your program should do the following: 1....

    Note wordBank, an array of 10 strings (char *s). Your program should do the following: 1. Select a word at random from the wordBank. This is done for you. 2. On each turn display the word, with letters not yet guessed showing as *'s, and letters that have been guessed showing in their correct location 3. The user should have 10 attempts (?lives?). Each unsuccessful guess costs one attempt. Successful guesses do NOT count as a turn. 4. You must...

  • Homework 02-Vectors, Strings, and Masking Function Name: forecast Inputs 1. (char) A character vector of the current fo...

    Homework 02-Vectors, Strings, and Masking Function Name: forecast Inputs 1. (char) A character vector of the current forecast 2. (char) The word to be replaced 3. (char) The new word to put into the forecast Outputs 1. (char) The updated forecast description Banned Functions strrep), replace), regexprep() Background You have recently started your internship as a weather engineer at the local news station. Unhappy with how the curment forecasts are described as 'gloomy" and 'cold, you decide to use MATLAB...

  • Write a C program that prompts the user to enter a series of integers that represent...

    Write a C program that prompts the user to enter a series of integers that represent a coded message. I have given you the decoder program so that you can see how it is written using array notation. Your assignment is to convert the code to a program that uses only pointers and pointer math. Your program must use pointer notation and pointer math (i.e. no array index notation other than in the declaration section, no arrays of pointers and...

  • True or False: Algorithms should be specified in pseudocode before they are implemented in C

     17.True or False: Algorithms should be specified in pseudocode before they are implemented in C.  18. True or False: In C, false is defined as any result that is zero. 19. True or False: An advantage to defining functions in C is that they may be reused in other projects. 20. True or False: Suppose a function main calls a function f. As a parameter to f, main passes a pointer p to a struct. Further suppose that f's body modifies p by...

  • Write a static method called encodeString that takes a string as input and prints to the...

    Write a static method called encodeString that takes a string as input and prints to the console a word where each letter is advanced by one value. In addition, your method should return the encoded String. You may make use of the method provided below to help you. public static char encode(char x) { int current = x; current++; return (char)current; } This method takes a character and returns an "encoded" character. In other words, encode('a') will return 'b'. Your...

  • 10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string...

    10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string object arguments. Let's call them string1, string2, and string3. It should search string for all occurrences of string2. When it finds an occurrence of Programming Challenges string2, it should replace it with string. For example, suppose the three arguments have the following values: stringt: "the dog jumped over the fence" string 2 "the" string3: "that" With these three arguments, the function would return a...

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