![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4
![]() |
replace space with ' * '
If the user input a space the output should be replaced with a ' * ', Not seems to except, I have added the value 32 which represent the space value on the ASCII table,However; if I replace the value of 32 to 33 which represent ' ! ' char, it would work, Any idea!!
void strencypt(char str[])
{
char star='*';
char str2[10];
int i;
for(i=0;((i<10) && (str[i]!='\0'));i++)
{
if ((int)(str[i])==32)
str2[i]= star;
else str2[i]=(int)(str[i]+1);
}
std::cout << "String A Is Encrypted Now With This Format: " << "\n";
for(i=0;((i<10) && (str[i]!='\0'));i++)
{
std::cout << str2[i];
}
std::cout << "\n";
}
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. |
|
|
|
|
|
#2 |
|
Programmer
|
Hoping I understand this right, why don't you loop through the string, using strlen for the counter termination, and then using something simple like "if(str[i] == ' ') str2[i] = '*';" ? I did something similar to this in a previous project where I replaced a space with a _ for writing/reading files.
kirkl_uk |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4
![]() |
I have tried that before and I ended up with the same results, The loop never stops .....
void strencypt(char str[])
{
char star='*';
char str2[10];
int i;
for(i=0;((i<10) && (str[i]!='\0'));i++)
{
if((str[i])== ' ') str2[i]= star;
else str2[i]=int(str[i]+1);
}
std::cout << "String A Is Encrypted Now With This Format: " << "\n";
for(i=0;((i<10) && (str[i]!='\0'));i++)
{
std::cout << str2[i];
}
std::cout << "\n";
}
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. |
|
|
|
|
|
#4 |
|
Programmer
|
Why do you limit str2 to 10 chars? I think I need more info to help on this one.
MSN me if you like kirkl_uk@hotmail.com Last edited by kirkl_uk; Apr 13th, 2005 at 3:56 AM. Reason: Added information |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4
![]() |
The Problem is not the size of the array!!!! its to replace the space with a ' * '!!!!!
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. |
|
|
|
|
|
#6 |
|
Programmer
|
I am aware of this. However, your explanation of the problem is very poor. Note how no one is able to help.
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4
![]() |
Is it!!! I dont see any hard part on it, However;
The problem with this code is the loop never stops when it reaches the space character. am not sure if the problem is my compiler This is the output of entering the string "hell o" EXECUTING: /home/moderator/C++/project2/main/readstri ---------------------------------------------- Enter 1 : To Read In A String Enter 2 : To Find The Size Of The String Enter 3 : To Copy String A into Sting B Enter 4 : To Merger String A With Sting B Enter 5 : To Compare Two Strings Enter 6 : To Encrypt String A, Method A Enter 7 : To Encrypt String A, Method B Enter 8 : To Locate a Character Enter 9 : To Exit The Program 6 Enter A String hell o String A Is Encrypted Now With This Format: ifmm Enter 1 : To Read In A String Enter 2 : To Find The Size Of The String Enter 3 : To Copy String A into Sting B Enter 4 : To Merger String A With Sting B Enter 5 : To Compare Two Strings Enter 6 : To Encrypt String A, Method A Enter 7 : To Encrypt String A, Method B Enter 8 : To Locate a Character Enter 9 : To Exit The Program 6 Enter A String hell o String A Is Encrypted Now With This Format: ifmm Enter 1 : To Read In A String Enter 2 : To Find The Size Of The String Enter 3 : To Copy String A into Sting B Enter 4 : To Merger String A With Sting B Enter 5 : To Compare Two Strings Enter 6 : To Encrypt String A, Method A Enter 7 : To Encrypt String A, Method B Enter 8 : To Locate a Character Enter 9 : To Exit The Program 6 Enter A String hell o String A Is Encrypted Now With This Format: ifmm Enter 1 : To Read In A String Enter 2 : To Find The Size Of The String Enter 3 : To Copy String A into Sting B Enter 4 : To Merger String A With Sting B Enter 5 : To Compare Two Strings Enter 6 : To Encrypt String A, Method A Enter 7 : To Encrypt String A, Method B Enter 8 : To Locate a Character Enter 9 : To Exit The Program 6 Enter A String hell o String A Is Encrypted Now With This Format: ifmm . . . . . . The loop wont stop running, if you complie it you will see what I mean void strencypt(char str[])
{
char star='*';
char str2[10];
int i;
for(i=0;((i<10) && (str[i]!='\0'));i++)
{
if((str[i])== ' ') str2[i]= star;
else str2[i]=int(str[i]+1);
}
std::cout << "String A Is Encrypted Now With This Format: " << "\n";
for(i=0;((i<10) && (str[i]!='\0'));i++)
{
std::cout << str2[i];
}
std::cout << "\n";
}and this is the part is on my main body case 6:
{
std::cout << "Enter A String" << "\n";
std::cin >> name;
strencypt(name);
break;
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. |
|
|
|
|
|
#8 |
|
Programmer
|
We are here to help, so try not to get so defensive. It's clear to you how the program should work and what happens during execution, but it wasn't clear for an outsider to the program. I don't have a compiler on this machine at the moment, so I will have a look at this when I get home later tonight. Remember, I am trying to help you.
|
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4
![]() |
kirkl_UK I wasn't trying to offend you, however; if I did please accept my apology.
some how I think I need to use the command gets instead of cin!!!Not sure though I'll try it.
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. Last edited by TecBrain; Apr 13th, 2005 at 8:09 AM. |
|
|
|
|
|
#10 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
wierd.. you thought of the answer but don't know why.
cin >> name; // reads up to whitespace, newline , or EOF so you were never reading in a space to begin with. cin.getline(name, length); // reads up to newline, or EOF Last edited by spydoor; Apr 13th, 2005 at 9:21 AM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|