|
[Java] Task 1
|
|
11-26-2009, 06:50 AM
Post: #1
|
|||
|
|||
|
[Java] Task 1
Some of these tasks I will post don’t have title so I will just name them Task <number>.
Task: You enter two variables, string and a char. Make a program which will check if the entered character is in the entered string. If yes, make a new string, in which the entered char, present in the entered string, will be put in brackets. Sample input: mathematics m Sample output: (m)athe(m)atics Java code: Code: import cs1.*;![]()
|
|||
|
11-26-2009, 07:02 AM
Post: #2
|
|||
|
|||
|
RE: [Java] Task 1
Very good done. String.charAt function exists in C++, but in pascal you need to treat string as an array of characters.
Good solution ! "I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein |
|||
|
11-26-2009, 07:09 AM
Post: #3
|
|||
|
|||
|
RE: [Java] Task 1
reply or if you use delphi or free pascal compiler they have the function Pos() to retrieve the index within a string.
|
|||
|
11-26-2009, 07:17 AM
Post: #4
|
|||
|
|||
|
RE: [Java] Task 1
Yeah certainly pos() function is for that, i have solution in Pascal using Pos function :
Code: program string_and_char;And also about nature of pos (position of string) which returns integer value which represents position of sub string in string. If it returns 0 that means that char "k" is not inside of string "s", and if it is bigger than zero than it is okay, and that is this line : Code: if pos(k,s)>0 thenhttp://www.pro9ramming.com/learnpascal/strings.html "I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein |
|||
|
11-26-2009, 07:19 AM
(This post was last modified: 11-26-2009 08:07 AM by codecaine.)
Post: #5
|
|||
|
|||
|
RE: [Java] Task 1
Nice work!
![]() python style Code: tempStr = raw_input("Enter a string: ");I forgot that delphi and fpc had a AnsiReplaceStr(). You can do it the exact same way as I did it in python My java version Code: import java.util.Scanner;Showing of the power of unix console this will work on mac or linux ![]() Code: echo Enter a stringI could of done ${mychar:0:1} to just to return the first character of the string, but I didn't want it to look to complex |
|||
|
11-26-2009, 08:45 AM
Post: #6
|
|||
|
|||
|
RE: [Java] Task 1
Wow Python is total ownage.
It seems so simple and yet so powerful. ![]() Though I still don't understand this code, I tried it. I'm using Python 2.5. ![]()
|
|||
|
11-26-2009, 08:54 AM
Post: #7
|
|||
|
|||
| RE: [Java] Task 1 | |||
|
11-26-2009, 08:57 AM
Post: #8
|
|||
|
|||
|
RE: [Java] Task 1
its about this line:
Code: parsedStr = tempStr.replace(tempChar[0],'(' + tempChar[0] + ')')what this operation 'replace' does actually? and why do we have [0] in both of these places? ![]() hope this is not a nooby question, i'm a beginner in python and i just tried your code. ![]()
|
|||
|
11-26-2009, 08:57 AM
Post: #9
|
|||
|
|||
|
RE: [Java] Task 1
tempChar[0] part is because I only want the first character from the string, and not the whole string the user input if they entered more then 1 characters.
|
|||
|
11-26-2009, 09:05 AM
(This post was last modified: 11-26-2009 09:05 AM by Alphablend.)
Post: #10
|
|||
|
|||
RE: [Java] Task 1
(11-26-2009 08:57 AM)codecaine Wrote: tempChar[0] part is because I only want the first character from the string, and not the whole string the user input if they entered more then 1 characters. oh i get it. its simple. ![]() i just had to turn on my brain for a sec. ooh... i'm liking this language. ![]() thanks.
![]()
|
|||
|
« Next Oldest | Next Newest »
|





![[Image: 45669_pythonlogo.png]](http://myph.us/pics/45669_pythonlogo.png)






this will work on mac or linux 
