Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,495 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,347 people online right now. Registration is fast and FREE... Join Now!




Program to replace 'a' with '@', and 's' with

2 Pages V  1 2 >  
Reply to this topicStart new topic

Program to replace 'a' with '@', and 's' with

Captain M
23 Feb, 2007 - 02:11 PM
Post #1

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 95


My Contributions
I have an assignment that says I need to replace all 'a's with '@', and all s's with '$'. The program needs to be able to ask the user to input a string, look through the string like an array and find all the a's and s's and replace them respectively. I am not sure how to do this, but I know it has something to do with .length. Here is my code so far:
CODE

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
if (userinput [x] == 'a'){
    userinput [x] = '@'}
else if (userinput [x] == 's'){
    userinput [x] == '$'}


  return EXIT_SUCCESS;
}

User is offlineProfile CardPM
+Quote Post

Captain M
RE: Program To Replace 'a' With '@', And 's' With
23 Feb, 2007 - 02:31 PM
Post #2

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 95


My Contributions
Ok, I did a little more research, and I'm pretty sure that I need to make a char variable, and an x variable, and do x ++ so it will look at each letter. The big part I'm confused with is the .length. I really don't get it! Do I need to make it say 100, and then the program will automatically put a null operator at the end?

User is offlineProfile CardPM
+Quote Post

Captain M
RE: Program To Replace 'a' With '@', And 's' With
23 Feb, 2007 - 02:42 PM
Post #3

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 95


My Contributions
Updated code:
CODE
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{string userinput;
cout << "Type a sentance please./n";
getline (userinput);
if (userinput [x] == 'a'){
    userinput [x] = '@'}
else if (userinput [x] == 's'){
    userinput [x] == '$'}


  return EXIT_SUCCESS;
}

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Program To Replace 'a' With '@', And 's' With
23 Feb, 2007 - 02:57 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,303



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
the function length returns the number of characters in a string.

You are on the right track. You will need a loop to iterate through all the characters in the string. That is where length comes in, it tells you how many times you must loop in order to go through the entire sequence of characters.

One note though the syntax of the getline function in this situation is it requires two parameters. The first is an input stream, in this case cin is the input stream and the second parameter is the variable to store the user input into.
CODE

getline (cin, userinput);

User is online!Profile CardPM
+Quote Post

Captain M
RE: Program To Replace 'a' With '@', And 's' With
23 Feb, 2007 - 03:05 PM
Post #5

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 95


My Contributions
Here's the updated code. Is it close to the right track?
CODE
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{string userinput [x];

cout << "Type a sentance please./n";

getline (cin, userinput);
do{
if (userinput [x] == 'a'){
    userinput [x] = '@';}
else if (userinput [x] == 's'){
    userinput [x] == '$';}
}while (

  return EXIT_SUCCESS;
}


This post has been edited by Captain M: 23 Feb, 2007 - 03:08 PM
User is offlineProfile CardPM
+Quote Post

lattyware
RE: Program To Replace 'a' With '@', And 's' With
23 Feb, 2007 - 03:07 PM
Post #6

D.I.C Head
Group Icon

Joined: 8 Aug, 2005
Posts: 55


Dream Kudos: 75
My Contributions
You are missing a big part.

Note this is all in pseudo-code. Nothing real. Just to give you the idea.

You have your string.

CODE
input = "How are you son?"

input = ["H","o","w"," ","a","r","e"," ","y","o","u"," ","s","o","n","?"]


Yes?

OK, so what you need to do is go through each character and check if it needs to be replaced so:

CODE
while (x < input.length):
    if (input[x].lower = "a"):
        input[x] = @
    if (input[x].lower = "s"):
        input[x] = $


note I added .lower as a sign that you need to check for both cases, which I presume is true.

So at the moment, this code will go like this.

x = 0

x is less than 15

the letter at position 0 is "H"

this isn't a

this isn't s

now, this will just check H again and again.

We need to make it:
CODE

while (x < input.length):
    if (input[x].lower = "a"):
        input[x] = @
    if (input[x].lower = "s"):
        input[x] = $
    x++
    
print input


this will increment x, so the next time it loops, it checks the next character, until it has completed them all, then prints the string.

Hope that helps.

Edit: Ah, someone else responded. Oh well.

This post has been edited by lattyware: 23 Feb, 2007 - 03:08 PM
User is offlineProfile CardPM
+Quote Post

Captain M
RE: Program To Replace 'a' With '@', And 's' With
23 Feb, 2007 - 03:17 PM
Post #7

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 95


My Contributions
Ok, but I still get this:
CODE

replace.cpp: In function ‘int main(int, char**)’:
replace.cpp:13: error: ‘x’ was not declared in this scope

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Program To Replace 'a' With '@', And 's' With
23 Feb, 2007 - 03:21 PM
Post #8

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,303



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Again you are on the right track. Although I would probably use a WHILE loop as opposed to a DO/WHILE loop. But it is your choice.

Now you just need a test condition inside the WHILE portion of the loop and an incrementing variable that increments inside the loop by one each time through.

You need to fix this part:
CODE

{string userinput;

User is online!Profile CardPM
+Quote Post

Captain M
RE: Program To Replace 'a' With '@', And 's' With
23 Feb, 2007 - 03:22 PM
Post #9

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 95


My Contributions
Sorry, here is the updated code:
CODE

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
string userinput [input];

cout << "Type a sentance please./n";

getline (cin, userinput);

while (x < input.length) {
if (userinput [x].lower == 'a'){
    userinput [x] = '@';}
else if (userinput [x].lower == 's'){
    userinput [x] = '$';}
}

  return EXIT_SUCCESS;
}

User is offlineProfile CardPM
+Quote Post

lattyware
RE: Program To Replace 'a' With '@', And 's' With
23 Feb, 2007 - 03:24 PM
Post #10

D.I.C Head
Group Icon

Joined: 8 Aug, 2005
Posts: 55


Dream Kudos: 75
My Contributions
You need an x = 0 and an x++.
User is offlineProfile CardPM
+Quote Post

Captain M
RE: Program To Replace 'a' With '@', And 's' With
23 Feb, 2007 - 03:38 PM
Post #11

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 95


My Contributions
Ok, so here's my code:
CODE

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
string userinput [x];
char x = 0;

cout << "Type a sentance please./n";

getline (cin, userinput);

while (x < userinput.length) {
if (userinput [x].lower == 'a'){
    userinput [x] = '@';}
else if (userinput [x].lower == 's'){
    userinput [x] = '$';}
x ++;
}

  return EXIT_SUCCESS;
}


And here is the errors:
CODE
replace.cpp: In function ‘int main(int, char**)’:
replace.cpp:8: error: ‘x’ was not declared in this scope
replace.cpp:9: error: redeclaration of ‘char x’
replace.cpp:8: error: ‘<typeprefixerror>x’ previously declared here
replace.cpp:13: error: ‘userinput’ was not declared in this scope


I'm thinking there is a problem with username parameters.
User is offlineProfile CardPM
+Quote Post

Captain M
RE: Program To Replace 'a' With '@', And 's' With
23 Feb, 2007 - 04:20 PM
Post #12

D.I.C Head
**

Joined: 21 Jan, 2007
Posts: 95


My Contributions
Anybody? There are 2 other people reading this thread. C'mon!
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 1/7/09 06:06PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month