C/C++ :: Replacing Character In String With Another Character

Sep 13, 2014

So I'm trying to create a function that replaces any instance of a character in a string with another. So first I tried the replace() string member function:

In my implementation file

void NewString::ReplaceChar(const char& target,const char& entry)
{
this->replace(this->begin(),this->end(), target, entry);
};

Main program

#include "NewString.h"
using namespace ...;
int main()

[Code].....

Instead of replacing the the l's with y's it outputted a long string of y's. Also, NewString is derived from the string class (it's for the assignment). the header and whole implementation file, already tested.

I've also tried, instead, to use a for loop in ReplaceChar() but I need to overload the == operator and I don't know how I should exactly:

bool NewString::operator ==(const char& target)const {
if(*this == target)
return true;

[Code]....

I want the == operator to test if the value in the char array is equal to target but I'm not sure how to pass in the position. I'm guessing the this pointer in ReplaceChar() is not the same as the one dereferenced in ==() because target is never replaced by entry in the string.

View 5 Replies


ADVERTISEMENT

C :: Reads And Compress Each Row Of Array By Replacing Each Character

Mar 1, 2014

Code for c programming reads and compress each row of the array by replacing each character with a single character and the number of times it occurs?

I have issue in doing this.

View 5 Replies View Related

C++ :: Multi-character Character Constant Error Message?

Sep 14, 2014

I keep getting this warning message and I do not know how to fix it. Is it because I'm using char to instead of strings to replace all 't' with 'lp'?

#include<iostream>
#include<string>
#include <stdio.h>
using namespace std;
char * scanf(char * a) {

[code]....

View 6 Replies View Related

C++ :: Reading Data Character By Character From Text File

Jul 25, 2012

Double values are stored in text file. 23.5 36.8 34.2 ... My teacher told me to read them character by character and then make words, like i have to read "2" "3" "." "5" and now have to make it or treat it as word and then using atoi(). I have to convert it into double. but i dont know how to do this....

View 5 Replies View Related

C :: Read From Stdin (File) Character By Character

Nov 10, 2013

I have to optimize a code for below scenario. I am reading stdin (a file redirected to stdin) character by character. How many chars are going to come is not known. After every few chars there is a seaparator. e.g $ as below

rhhrkkj$hghjhdf$ddfkrjt

While reading, if the separator arrives I'm processing the string stored before that separator and then continue reading stdin in same fashion, till EOF. I am using getc(stdin) to read chars.

Using gprof I can see most of the program time is spent inside main() , for this reading logic. Rest of the program is just some insert and search operations. I am getting time of 0.01 secs at the moment, want to reduce further.

View 6 Replies View Related

C/C++ :: Read Input File Character By Character?

Aug 10, 2012

How do I write an a program that will read an input file character by character?

View 1 Replies View Related

C++ :: How To Read TXT File Character By Character

Oct 6, 2013

In my program, I'm supposed to read a text file (the name of which is given to me as a command line paramater, as long with an integer), and display the text in a specific format (each line can only be as long as the integer). However, I'm having trouble even reading the text file. I don't know the syntax. I'm only allowed to edit the function that does the formatting, and the code in that is

void typeset (int maxWidth, istream& documentIn)

I don't know how to 'read' the file, as most examples online are ifstream, or openFile or something like that. What I want to do is just read the first character of the file, and continuously keep reading characters until the end of the file.

View 10 Replies View Related

C# :: How To Get Int Value Of Each Character In A String And Then Add Them All Together

Aug 20, 2012

I'm trying to get the int value of each character in a string and then add them all together so I can do a 1's complement of the total value. I'm trying to do simple checkum kinda of thing for verification of data.

For example: string DPacket = "Hello World!";

I would like to have each character added and do the ones complement. Will it be easier to convert first to int and then add or any other easier way? So my result should be the decimal value addition of each character and then do the ones complement to that.

View 5 Replies View Related

C++ :: String Character Removal

Dec 25, 2013

I know how to remove certain characters from a string by using something like this:

Code: string str ("Hello world!");
erase (0, 6);

That's great if I want to do that manually, but say if someone entered a string, how would I automatically remove every other character they entered?

View 4 Replies View Related

C :: Check Certain Character Is In String Or Not?

Mar 6, 2015

I want to check whether a certain character is in a string or not but my code is not working

Code:
#include<stdio.h>
#include<string.h>
int main()
{

[Code].....

View 7 Replies View Related

C :: Insert Character To A String

Feb 26, 2013

Let's say i have a string "file.txt" and i want to insert "_out" to make it look like "file_out.txt"

I don't know how to do this ....

View 8 Replies View Related

C :: Copy A Character To String

Mar 6, 2015

Copy some characters from char * arg to char * first using a loop with specific conditions.

Code:

char * arg;
// set arg some string...
char first_[25];
char * first;
int length;
length=strlen(arg);
for (n++; arg[n] != '}' || n>=length-1; n++)
strcpy(first,arg[n]); // first += arg[n]; I have strcpy(first,arg[n]); but arg[n] is char and strcpy expects char * ;

how to solve this?

View 2 Replies View Related

C :: Display One Character In A String?

Jun 14, 2013

It's been about two years since I last program c, now I need to do it for a basic project. How would I print out one letter in a string?

For example lets say I have a string called str=[Hello]. I want to display the third letter so just the "l". Here's what I have so far:

Code:

#include<stdio.h>
int main() {
char str[50] = "Hello";
printf("The third letter is : %s
",str[3]);
return 0;
}

View 2 Replies View Related

C++ :: Searching For Character In String

Jun 19, 2013

I'm trying to find a < character in a document, get it's position. Then find > and get it's position. Then i want to delete all things between that but runtime is terminating my process so i don't know what to do.

The code:

#include <iostream>
#include <conio.h>
#include <stdio.h>

[Code].....

View 6 Replies View Related

C++ :: Character Displaying From A String?

Jan 27, 2013

I want to input a string, say: abcdaa so, the program should output:

a
b
c
d

In other words, the program will display each character for only ONCE!!!! And display their frequency. Here is my idea: user will input a string and such string will be copied into another string variable called "checker".There will be a loop and each character will be printed, BUT, first, the program will check if the character to be printed is not equals to all elements of the checker string.

I already have the function to count the frequency of each character

GOAL: to make a program that will accept a string and use the HUFFMAN CODING to compress it.

for(x=0; x<string_in.size(); x++) {
cout<<" "<<string_in[x]<<endl;
for(y=0; y<string_in.size(); y++) {
if(checker[y]==string_in[x])
break;
else
checker[x]=string_in[x];
}
}

View 13 Replies View Related

C# :: How To Replace Character In String

Feb 12, 2015

I have an open file dialog that opend the xml file and store the path to the textbox, it returns the path correctly but when i need to store that xml file into the database it tells me that there is an error next to '' because when i try to debug it it gives me "C:\Student Results\FC2015.xml" this results then it breaks. here is my code:

This code returns xml path to textbox

OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "XML Files (*.xml)|*.xml";
ofd.FilterIndex = 0;

[Code]....

View 2 Replies View Related

C/C++ :: Remove Character In String?

Mar 1, 2014

I need to make a function that removes a function in a c-string. This is what I have:

#include <iostream>
using namespace std;
char removeCharacter (char *str, char c)
{

[Code].....

View 5 Replies View Related

C++ :: Output Last Character In C-String

Jun 9, 2013

I keep getting this error after I input:

Code:
Unhandled exception at 0x54AE350B (msvcp110d.dll) in Random.exe: 0xC0000005: Access violation writing location 0x0131CA21.

Code:
#include <iostream>
#include <cstring>
//Prototypes
void lastChar(char *);
int main() {
const int LENGTH = 21;

[Code] ....

It builds without any errors. I am just trying to output the last character in the C-string. Am I doing this all wrong?

View 14 Replies View Related

C :: Convert A Character Directly To A String

Nov 25, 2013

I wish to convert a character directly to a string for a top-secret project I'm working on. It needs to be portable across various machines with different sized Indians.

Code:

#include <stdio.h>
int main(void)
{
const int i = 0x0041;
const char *str_p = (char *) &i;
}

[code]....

I want this to output an 'A', but I'm not sure this code will work on my friend's mom's S/360.

View 2 Replies View Related

C++ :: Character Array To Wide String?

Nov 19, 2013

I am still working on my project which will be reading some old data from some old DOS files. The data stored there is naturally, char*. Once I read in my character array, how do I assign this to a wstring since my application is UNICODE?

Here is my current solution:

wchar_t* Class::Function(char *pName) {
//I verify the pointer and such first, then do the below
this->_Name.assign(pName, (pName + strlen(pName));
return this->_Name.c_str();
}

Am I on the right track here?

View 9 Replies View Related

C++ :: Convert From Integer To Character String

Oct 2, 2013

I have been trying to write a function which can convert a number from an unsigned long integer to a readable ASCII character string. this is what I have come up with, but I am receiving some very strange characters in return. Could the problem be that I am telling a char to = an unsigned long int, (cString[i] = product[i])?

void convertToString(unsigned long con) {
unsigned long product[10];
char cString[10];
const unsigned long begConvert = 10 ^ 10;

[Code] ....

View 4 Replies View Related

C++ ::  Finding Most Common Character Within A String

Oct 24, 2013

I am trying to take a string that is within the main function, and write a void function that gives me the most common alpha character used inside the string. How to mix a string and an array together like that as I am not too familiar with arrays yet.

View 8 Replies View Related

C++ :: Use Of A Backspace Character In A String Literal

Apr 19, 2014

In the following char array, notice the use of a backspace character in a string literal: ''.

char text1[50] = "aHello,
World! Mistakee was "Extra 'e'"!
";

What exactly does a backspace character do here? When the compiler evaluates this line, does it actually delete the previous character, like when you press the backspace button on the keyboard?

View 1 Replies View Related

C++ :: How To Find Position Of Certain Character In String

Jan 11, 2013

I need to find position of certain character in string, so i made a function but it doesn't do what i thought it would.

Consider this string:

std::string line = "<foo> <foo> <foo> <foo> <foo>"

I need to find position of a third '>' character so i can get:

std::string other = "<foo> <foo> <foo>";

This is my function that fails:

std::size_t find_third(const std::string& line) {
std::size_t pos = 0u;
for(std::size_t i = 0; i < 3u; ++i) {
std::string tmp = line.substr(pos);
pos = tmp.find_first_of('>');
} return pos;
}

View 6 Replies View Related

C++ :: Error Reading Character Of String?

Jan 12, 2013

I am trying to record some information in a file and allow user to delete a record. I am facing this message in Autos section of MVS (Error reading character of string). Here is the code:

int removeRecord(string name, int &row)//remove a record
{
const string data="database.txt";
fstream records;
records.open(data.c_str());
const string cpData="temp.txt";

[code].....

If I delete the any row (except last roe) it works but then add a copy of last record (sometimes fully sometimes partially) at the end of the file!! if i delete the last record it does not do anything.

View 1 Replies View Related

C++ :: Insert Character After Each Number In A String

Nov 30, 2013

I would like to insert some character after each number of my string .. I found how to insert before each character, but what I want is :

string str = "12 + 5"

insert ^ after each number

Output = "12^ + 5^"

View 7 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved