C/C++ :: Implement String Class And Modify Specific Char
May 4, 2014
I tried to implement a string class and i want to modify a specific char in my string . I did operator over load but this code crash in this line
name [2] = 'k' ;
Why ?!!!!!!!!!!!!!
this is my code
# include <iostream>
using namespace std;
#include <assert.h>
class String {
public :
char *content ;
int size ;
[Code] ....
View 11 Replies
ADVERTISEMENT
Jul 5, 2013
int index = -1;
string NewStr = null;
char[] lower = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char[] upper = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
foreach (char c in str)
[Code]....
The code above loops through a string and for each character checks to see if it is a lower case character, then checks to see if it an upper case. It then removes it if it is. Leaving only the numbers.
However, it reuses the same string the entire time, never updating the string so it always finds the same (first) character.
View 6 Replies
View Related
Feb 5, 2013
A string class that finds a char in the string?
View 1 Replies
View Related
Oct 8, 2014
i have to write a function to modify the input string by substituting each alphabetical character. for example, 'Programming' to 'Rtqitcookpi' (mod_int = 2).
View 1 Replies
View Related
Oct 29, 2014
Code:
cout<<"Enter Filename for input e.g(inp1.txt .... inp10.txt):"<<flush;
cin>>filename;
ifstream inpfile;
inpfile.open(filename,ios::in);
if(inpfile.is_open())
[Code] .....
View 8 Replies
View Related
Aug 20, 2014
I'm playing with the idea of a singleton base class, but I'm having an issue with how to implement the GetInstance() function in the base class. Since I'm trying to make this ridiculously simple for the child, I'd like to handle that in the base.
class Singleton {
private:
static Singleton* instance;
Singleton() { Construct(); } // Private to avoid other instances
[Code] .....
It would be easy to use like so:
class Hello : public Singleton {
private:
std::string hello;
void Construct() { hello = "hello"; }
public:
std::string GetHello() const { return hello; }
};
Then the instance would be handled like so:
std::cout << Hello::GetInstance()->GetHello();
View 12 Replies
View Related
Aug 17, 2013
I am supposed to implement the member functions of class Person.
class Person {
public:
Person();
Person(string pname, int page);
void get_name() const;
void get_age() const;
[Code] ....
The code I wrote is below. Where I am struggling is the program does not allow me to input age. Therefore, I cannot test if my temp for age works. It automatically defaults to 0 because it hasn't taken input. Here is my code:
// Program Title: Person function
// Program Description: The program prompts the user for first and last name and age.
// It then prints the output that was provided by the user.
#include<iostream>
#include<string>
using namespace std;
class Person {
[Code] .....
View 13 Replies
View Related
May 22, 2013
So I have a class that is like this:
class card {
public:
int id;
int val;
};
card card1;
card1.id = 1;
card1.val = 2;
card card2;
card2.id = 2;
card2.val = 45;
etc...
So my question is firstly, is there a better way to implement this? (a vector of classes or something maybe?) and how can I call up a specific instance of the class. For example, if I want the val of a specific instance of the class, how best can I do that?
View 3 Replies
View Related
Jun 15, 2013
Well, basically, what I've been doing was creating a class that would implement the concept of Double Linked List, but that would behave like a queue ( the STL implementation is deque, or Double Ended Queue ).
The problem occured when I have been trying to generalize the class using templates. I mean, why use only integers ? Why not double or char or what not ?
Worked perfectly before including all the template stuff..
// Source.cpp <=> Main.cpp
#include <iostream>
#include "DList.h"
using namespace std;
int main(void) {
DList<int> *list;
list = new DList<int>();
[Code] .....
The errors returned by the compiler:
Error1error C2955: 'Node' : use of class template requires template argument listc:usersjumperdesktopc++ otherdouble linked listdouble linked listdlist.h6
Error2error C2955: 'Node' : use of class template requires template argument listc:usersjumperdesktopc++ otherdouble linked listdouble linked listdlist.h6
View 6 Replies
View Related
Nov 29, 2014
Design, implement, and test a class that represents an amount of time in minutes and seconds. The class should provide a constructor that sets the time to a specified number of minutes and seconds. The default constructor should create an object for a time of zero minutes and zero seconds. The class should provide observers that return the minutes and the seconds separately, and an observer that returns the total time in seconds (minutes x 60 + seconds). Boolean comparison observers should be provided that test whether two times are equal, one time is greater than the other, or one time is less than the other. (You may use RelationType and function ComparedTo if you choose). A function should be provided that adds one time to another, and another function that subtracts one time from another. The class should not allow negative times (subtraction of more time than is currently stored should result in a time of 0:00). This class should be immutable.
this is one of my main errors: Error1error C2653: 'Time' : is not a class or namespace namec:userskdesktop
oane statecisp 1610visual studioschapter 12 assignmentchapter 12 assignmentchapter 12 assignment.cpp131Chapter 12 Assignment
//The implementation file ImplFileTimeClassAsgnt.cpp:
#include "Time.h"
#include <iostream>
using namespace std;
Time::Time() {
mins = 0;
secs = 0;
[Code] ....
View 2 Replies
View Related
Jan 23, 2013
I am trying to delete a speific element in an array of class objects. i am overwriting the element i waant to delete with the element after it. My algorithm works but the output is not correct, after debugging it seems my objects just dont copy, is there a way to copy a class object, i have looked up copy constructors and attempted to write one but it does not seem to have any effect on the output.
below is my code
class user {
string firstname, lastname, currentteam, position, status ;
int age ;
public:
user() {};
[Code] .....
View 4 Replies
View Related
Feb 4, 2015
i'm doing a validation exercise program. Just a question, how do i search an inputted string for a certain character?
For example:
Hello.World
I wanna determine and find out the where the '.' sign is.
I'm doing an if-else conditions, i need to determine where the character is located and make sure it doesn't repeat.
View 4 Replies
View Related
Feb 23, 2014
Suppose I have to take "100" from the string "1001", whats the process to do it?
View 6 Replies
View Related
Dec 30, 2013
I have `MainShop`(base class) then `SwordShop` and `BowShop`(derived classes) and another derived class called `Inventory`. I have a vector under protected in my base class, and when I access it from my derived classes it's okay, but each one has its own value. How can I set them all to have the same value?
//MainShop.h
#pragma once
class MainShop {
private:
//some variables
protected:
vector <string> WeaponInventory;
[code]......
Here every time I run it, it goes straight to the `else` and it displays that I do not have any items. I want to just have one vector for both my bow and Sword shops because vectors take up a lot of memory and my teacher said keep them to a minimum. So I just want one vector that takes in items from my bow class as well as my sword class, but for some reason it's acting as if I have multiple vectors, each with its own set of items.
View 3 Replies
View Related
Apr 21, 2013
I need reading a specific input syntax.
something like
<Input> :: R C <Content>
3
3
.3. .15 9.2
.1. ..6 ...
6.5 7.. ...
... .5. ..9
4.7 .8. 1.6
9.. .6. ...
... ..1 7.4
... 6.. .9.
5.1 49. .3.
View 2 Replies
View Related
Oct 16, 2014
I am attempting to write a recursive function that, given a string, recursively computes a new string where all the lowercase 'x' chars have been moved to the end of the string.
For example,
moveXs("xxre") --> "rexx"
moveXs("xxhixx") --> "hixxxx"
moveXs("xhixhix") --> "hihixxx"
Below is the code I have written thus far, but it seems to be returning only empty strings.
string moveXs(const string& str) {
string strCopy = str;
if (strCopy.length() <= 1) {
return str;
[Code] ....
View 6 Replies
View Related
Oct 15, 2013
I am trying to print a specific line from a textfile
e.g
I have a text file called info.txt and inside it contains the id,item name, price, quantity
Code:
1,shirt,100,10
2,pants,50,9
3,pen,20,8
I know how to find a specific word at the line in the file but how do I find and print out the specific line e.g(print out just the entire contents of line 2)?
Code:
string temDescription;
ofstream fout;
int curLine = 0;
[Code].....
View 1 Replies
View Related
Mar 5, 2013
How do I make a specific character show up a specific amount of times?
Like I am generating a random number then I need to make "|" show up that many times on the screen.
View 1 Replies
View Related
Feb 19, 2015
I'm trying to determine the number of times I have to change each specific character in a string to make it a palindrome. You can only change a character one at a time from the end.
Example: "abc" -> "abb" -> "aba" should print 2. "aba" will print 0 because it's already a palindrome. "abcd" -> "abcc" -> "abcb" -> "abca" -> "abba" will print 4 because it took 4 changes to make a palindrome.
I'm not too sure how to approach this - I figured out the case where if it's a palindrome (if reversed string is the same) then it'll print out a 0.
int main() {
int number;
cin >> number; //expecting a number for first line user input
for (int i = 0; i < number; i++) {
string str;
[Code] ....
View 1 Replies
View Related
Mar 27, 2013
i want to know how i can solve this question? do i need to create a class or write the program codes.
View 12 Replies
View Related
Dec 20, 2014
I have the codes for such a problem where, to create a program that counts how many times the second string appears on the first string. Yes it counts if you put 1 letter only, but if you put 2, it is an error. As an example. If the first string is Harry Partear, and the second string is ar, it must count as 3. Here's the code:
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
[Code] ....
View 6 Replies
View Related
Apr 20, 2013
I'm trying to "tokenize" a string using std::string functions, but I stored the text in a char array. When I try to convert it to a string, the string has the first character right but the rest is garbage.
// Get value from ListBox.
char selectedValue[256];
memset(selectedValue, NULL, 256);
SendMessage(GetDlgItem(hWnd, IDC_LB_CURRENTSCRIPT), LB_GETTEXT, selectedIndex, (LPARAM)selectedValue);
// Convert to string.
string val(selectedValue);
[Code] ....
View 3 Replies
View Related
Apr 5, 2013
I have this problem that I don't know how to sort out. I have a feeling it's really simple
#include <iostream>
#include <string>
void main () {
char test[10] = "gorilla";
char try = test[1];
cout << try;
}
I get the error:
lab4.cpp:6: error: expected primary-expression before "char"
lab4.cpp:6: error: expected `;' before "char""
View 2 Replies
View Related
Mar 4, 2013
how can I find ASCII value (0..255) of a character char in a string?
I have an array of char:
Code: char myarray[50]; and I need to have ASCII value of character eg myarray[10]. How can I do that?
View 1 Replies
View Related
Oct 19, 2013
I have some code:
char cHomeTeamFaceOffsPercentageWon[100];
memcpy(cHomeTeamFaceOffsPercentageWon,cSqlHomeTeamFaceOffsPercentageWon,100);
After this, for example, cHomeTeamFaceOffsPercentageWon is, "29%".
Then, I use
std::string szwPercentageWon = std::string(cHomeTeamFaceOffsPercentageWon);
szwPercentageWon is then, "2". Shouldn't this convert correctly, to "29%" as well.
Or is there something I'm missing? Maybe the termination character, or something.
View 1 Replies
View Related
Apr 30, 2013
Is there anyway to convert std::string to char*?
Like:
std::string x="hello world";
char* y=x;
View 6 Replies
View Related