22 Sep 2008

Programming exercise for newbies

Here's a small coding exercise for programming newbies. I asked this question in an interview to a computer science graduate and she could not solve it. It was surprizing. The computer science, IT engineers should start thinking on their own - mugging up the stuff from book doesn't help.

So here's a simple programming challenge [for newbies]:-

Write a program that accepts a new text strings, one on each line, but prints a line number before it accepts the input. The lines are then printed out, with each word individually reversed.

That is, the output should look like -
<Text string Input>

1: Hello, this is The Big K
2: This is line number 2
3: ___

*******************************
1: K giB ehT siht ,olloeH

etc.

I hope you got the problem. Use any damn computer programming language of your choice. The only bet is - you crack this question on your own. The purpose is to make you think, not to test your Google search skills.

All the best.

[Provide all details in comments inside your code]
Ashraf HZ

Ashraf HZ

Communications
22 Sep 2008
[spam]
*quickly closes Google Search*

uh.. say what? 😛

It'll be interesting to code this in a language you are not familiar with 😀
[/spam]
gaurav.bhorkar

gaurav.bhorkar

Branch Unspecified
22 Sep 2008
I do not know any computer language as I am in 1st year but still, i will try using HTML code and JavaScript.

<html>
<body>
<script language="JavaScript">
a=parse(prompt("text here"));
document.write(+a);
document.write(+"1: Hello, this is The Big K");
document.write(+"2: This is line number 2");
document.write(+"3: ___");
</script>
</body>
</html>


I guess it is correct.
chtn

chtn

Branch Unspecified
22 Sep 2008
hey can u select one best project in electronics
22 Sep 2008
gaurav.bhorkar
I do not know any computer language as I am in 1st year but still, i will try using HTML code and JavaScript.

<html>
<body>
<script language="JavaScript">
a=parse(prompt("text here"));
document.write(+a);
document.write(+"1: Hello, this is The Big K");
document.write(+"2: This is line number 2");
document.write(+"3: ___");
</script>
</body>
</html>

I guess it is correct.
Wow! I never expected such a simple solution! 😁

I guess another simple solution would be to use "printf" or "cout" keywords to generate the output. Right?
23 Sep 2008
Hah!

Guys! This one is simple! I want the code! Right NOW! [​IMG]
gaurav.bhorkar

gaurav.bhorkar

Branch Unspecified
24 Sep 2008
Can anyone display this :

*
**
***
****
*****
niraj.kumar

niraj.kumar

Branch Unspecified
24 Sep 2008
Gaurav this is very simple but good for newbie to computer science this problem should be like this
*
***
*****
*******
24 Sep 2008
I want the first problem to be cracked! No one yet?
anuragh27crony

anuragh27crony

Branch Unspecified
24 Sep 2008
Does the line numbers...include Input...????
Raviteja.g

Raviteja.g

Branch Unspecified
26 Sep 2008
sorry for the late respose i am writing the code in java hope this is correct if i am wrong please correct me.
import java.io.*;
class rev
{public static void main(String args[])
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br =new BufferedReader(isr);
String s;
System.out.println("input the string");
System.out.println("the line number is two");
s=br.readline;
System.out.println(s.reverse());
}
}
sriramchandrk

sriramchandrk

Branch Unspecified
26 Sep 2008
😔
The_Big_K
Here's a small coding exercise for programming newbies. I asked this question in an interview to a computer science graduate and she could not solve it. It was surprizing. The computer science, IT engineers should start thinking on their own - mugging up the stuff from book doesn't help.

So here's a simple programming challenge [for newbies]:-

Write a program that accepts a new text strings, one on each line, but prints a line number before it accepts the input. The lines are then printed out, with each word individually reversed.

That is, the output should look like -
<Text string Input>

1: Hello, this is The Big K
2: This is line number 2
3: ___

*******************************
1: K giB ehT siht ,olloeH

etc.

I hope you got the problem. Use any damn computer programming language of your choice. The only bet is - you crack this question on your own. The purpose is to make you think, not to test your Google search skills.

All the best.

[Provide all details in comments inside your code]
This work's is this what you wanted?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
char inp[255];
int i = 0;
vector<string> inpTxt;
string str;
cout << "Input lines to stop input 'end'" << endl;
while(strcmp(inp,"end"))
{
cout << ++i << ":" ;
cin.getline(inp, 255);
str.assign(inp);
reverse(str.begin(), str.end());
inpTxt.push_back(str);
}
cout << endl << "Reversed Output" << endl;
for(vector<string>::iterator it = inpTxt.begin(); it!= inpTxt.end(); it++)
cout << (*it).c_str() << endl;
}

==== TEST RUN =====
Input lines to stop input 'end'
1:who is this
2:i am not going to see google
3:i remember few things
4:bye
5:end
Reversed Output
siht si ohw
elgoog ees ot gniog ton ma i
sgniht wef rebmemer i
eyb
dne


Regards
Sriram
crazypooh

crazypooh

Branch Unspecified
05 Oct 2008
k the problem is not tough but 1 confusion that what do u mean by reversing the words??????
crazypooh

crazypooh

Branch Unspecified
05 Oct 2008
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
05 Oct 2008
Please read the question again. I've provided sample input/output. That should be enough to give you a hint.

Share this content on your social channels -

Only logged in users can reply.