RT programming without using "if else"

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 1856 Views - Last Post: 30 November 2012 - 05:54 AM Rate Topic: -----

#1 crippler  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 9
  • Joined: 29-November 12

RT programming without using "if else"

Posted 29 November 2012 - 08:08 AM

Hello, I have a question

I want to implement a function, which gets a string contains words.
between the words there are '\' character, and I want to replace it with
' ' (space) character.

example: good\morning
change to : good morning

There is a big limitation, I can't use "if else" conditions.

thank you
Is This A Good Question/Topic? 0
  • +

Replies To: RT programming without using "if else"

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,865
  • Joined: 25-December 09

Re: RT programming without using "if else"

Posted 29 November 2012 - 08:14 AM

What have you tried? Show the code that illustrates your problem.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 crippler  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 9
  • Joined: 29-November 12

Re: RT programming without using "if else"

Posted 29 November 2012 - 08:17 AM

View Postjimblumberg, on 29 November 2012 - 08:14 AM, said:

What have you tried? Show the code that illustrates your problem.

Jim



There is no code, I need to implement one
I can't think on a way to do so, without using if
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9366
  • View blog
  • Posts: 35,187
  • Joined: 12-June 08

Re: RT programming without using "if else"

Posted 29 November 2012 - 08:25 AM

Does this homework not allow you to use the string-replace?

http://www.cplusplus...string/replace/
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,865
  • Joined: 25-December 09

Re: RT programming without using "if else"

Posted 29 November 2012 - 08:26 AM

There are many different ways, but we need some code to see what language you are using and to give us some indication as to your level of experience.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: RT programming without using "if else"

Posted 29 November 2012 - 08:32 AM

Are you sure you want to do it with C/C++?

If not and javascript is your language, then your best bet is str.replace

regards,
Raghav

This post has been edited by raghav.naganathan: 29 November 2012 - 08:34 AM

Was This Post Helpful? 0
  • +
  • -

#7 crippler  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 9
  • Joined: 29-November 12

Re: RT programming without using "if else"

Posted 29 November 2012 - 08:40 AM

View Postmodi123_1, on 29 November 2012 - 08:25 AM, said:

Does this homework not allow you to use the string-replace?

http://www.cplusplus...string/replace/


It's not HW. I am prepering myself to entrance exams for an Embedded RT position.
My friends told me that they have been asked these kind of question.

No, I can't use this function, because I write in C, and not in C++.
Yet, I don't think that I can use any built-in functions.

View Postraghav.naganathan, on 29 November 2012 - 08:32 AM, said:

Are you sure you want to do it with C/C++?

If not and javascript is your language, then your best bet is str.replace

regards,
Raghav


The exam is in C language.
So, do you know any way to do so?
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7872
  • View blog
  • Posts: 13,353
  • Joined: 19-March 11

Re: RT programming without using "if else"

Posted 29 November 2012 - 08:42 AM

There are ways to do something like this in C. I can think of two that should work, and some candidate ideas that I'd have to play with. The point of something like this, though, is to implement it, not to watch someone else do it. So if you can't think of a way to do it, think of a way that you might try to do it - brainstorm a little. This is supposed to make you stretch out and understand the language better, so think about ways that apparently unrelated parts of the language might help you.
If you bring some idea to the table, even if it's not a good one, you'll find a much more helpful response.

Quote

I am prepering myself to entrance exams for an Embedded RT position.
My friends told me that they have been asked these kind of question.


So this exam is supposed to tell you and the examiners whether you're prepared for the position. Has it occurred to you that this exam might be actually telling you something, and that if you can't solve this sort of problem that you might actually not be suited to this position?

This post has been edited by jon.kiparsky: 29 November 2012 - 08:44 AM

Was This Post Helpful? 2
  • +
  • -

#9 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: RT programming without using "if else"

Posted 29 November 2012 - 09:04 AM

Well there's one... Two... Well, two easy ways that surely will work. Let me give you a hint.

Spoiler


Wait a sec - three... Four. Yup, four. Well five, but that last one is just ugly. That's of course with no inline asm, just C. :P/>
Spoiler


Ah, that's nice, sixth way would be my favourite as of now. No "cheating" like all those five ways above.
Spoiler


edit: Bah, there's obvious seventh way, though sixth is the best one yet. And I know you don't read my mind - I can post them all some time later, as to not spoil OP. ;)

This post has been edited by Xupicor: 29 November 2012 - 09:47 AM

Was This Post Helpful? 2
  • +
  • -

#10 crippler  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 9
  • Joined: 29-November 12

Re: RT programming without using "if else"

Posted 29 November 2012 - 09:18 AM

View PostXupicor, on 29 November 2012 - 09:04 AM, said:

Well there's one... Two... Well, two easy ways that surely will work. Let me give you a hint.

Spoiler


Wait a sec - three... Four. Yup, four. Well five, but that last one is just ugly. That's of course with no inline asm, just C. :P/>
Spoiler


Ah, that's nice, sixth way would be my favourite as of now. No "cheating" like all those five ways above.
Spoiler













arithmetic, bitwise, boolean, and pointers
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7872
  • View blog
  • Posts: 13,353
  • Joined: 19-March 11

Re: RT programming without using "if else"

Posted 29 November 2012 - 09:21 AM

View Postcrippler, on 29 November 2012 - 11:18 AM, said:

arithmetic, bitwise, boolean, and pointers



Yes, try all of those. And also think about what exactly " " and "/" are. Or rather, what ' ' and '/' are.

Hint.

Hint.
Was This Post Helpful? 0
  • +
  • -

#12 crippler  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 9
  • Joined: 29-November 12

Re: RT programming without using "if else"

Posted 29 November 2012 - 09:45 AM

I got it!!!!!!!

#include <stdio.h>

#define N 30
void main()

{

	char str[N];

	

	int i;
	int j;
	printf("Please enter a string: ");
	gets(str);
	
	printf("You entered the string:\n");

	
	puts(str);

	i=0;
	while(str[i] !='\0')
	{
		while(str[i] =='/' && str[i]!='\0')
		{
		str[i]=' ';
		i++;
		}
		i++;
		
		
	}

	printf("The new string is: \n");
	puts(str);



}

This post has been edited by jimblumberg: 29 November 2012 - 09:54 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Was This Post Helpful? 0
  • +
  • -

#13 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: RT programming without using "if else"

Posted 29 November 2012 - 09:51 AM

Very good effort indeed :)

Although you didn't use the variable 'j' which you declared, I think you have done a pretty good job on the logic aspect.

Next time, please use code tags and also indent your code for enhancing the readability. :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#14 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: RT programming without using "if else"

Posted 29 November 2012 - 09:52 AM

Never ever use gets(). It's unsafe. It's asking for trouble. It's deprecated in C99 and removed from C11*, and it's definitely not going to look good on your job interrogation interview.

Use code tags (see the red text on your editor?).
And by the way, you're not done yet. Where's the function?

*) gets_s was added though. AFAIK that's how it was. ;)

This post has been edited by Xupicor: 29 November 2012 - 10:05 AM

Was This Post Helpful? 1
  • +
  • -

#15 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7872
  • View blog
  • Posts: 13,353
  • Joined: 19-March 11

Re: RT programming without using "if else"

Posted 29 November 2012 - 09:53 AM

Yep, that's one way. Now do it again - backstroke this time.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3