Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 13th, 2005, 12:12 AM   #1
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4 TecBrain is on a distinguished road
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.
TecBrain is offline   Reply With Quote
Old Apr 13th, 2005, 3:02 AM   #2
kirkl_uk
Programmer
 
kirkl_uk's Avatar
 
Join Date: Apr 2005
Location: England
Posts: 86
Rep Power: 4 kirkl_uk is on a distinguished road
Send a message via MSN to kirkl_uk
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
kirkl_uk is offline   Reply With Quote
Old Apr 13th, 2005, 3:16 AM   #3
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4 TecBrain is on a distinguished road
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.
TecBrain is offline   Reply With Quote
Old Apr 13th, 2005, 3:43 AM   #4
kirkl_uk
Programmer
 
kirkl_uk's Avatar
 
Join Date: Apr 2005
Location: England
Posts: 86
Rep Power: 4 kirkl_uk is on a distinguished road
Send a message via MSN to kirkl_uk
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
kirkl_uk is offline   Reply With Quote
Old Apr 13th, 2005, 4:39 AM   #5
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4 TecBrain is on a distinguished road
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.
TecBrain is offline   Reply With Quote
Old Apr 13th, 2005, 4:49 AM   #6
kirkl_uk
Programmer
 
kirkl_uk's Avatar
 
Join Date: Apr 2005
Location: England
Posts: 86
Rep Power: 4 kirkl_uk is on a distinguished road
Send a message via MSN to kirkl_uk
I am aware of this. However, your explanation of the problem is very poor. Note how no one is able to help.
kirkl_uk is offline   Reply With Quote
Old Apr 13th, 2005, 6:35 AM   #7
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4 TecBrain is on a distinguished road
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.
TecBrain is offline   Reply With Quote
Old Apr 13th, 2005, 7:05 AM   #8
kirkl_uk
Programmer
 
kirkl_uk's Avatar
 
Join Date: Apr 2005
Location: England
Posts: 86
Rep Power: 4 kirkl_uk is on a distinguished road
Send a message via MSN to kirkl_uk
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.
kirkl_uk is offline   Reply With Quote
Old Apr 13th, 2005, 8:04 AM   #9
TecBrain
Hobbyist Programmer
 
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 4 TecBrain is on a distinguished road
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.
TecBrain is offline   Reply With Quote
Old Apr 13th, 2005, 9:14 AM   #10
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
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.
spydoor is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC