Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[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.*;
public class example{
    public static void main (String[]args){
        String s1=Keyboard.readString();
        char c1=Keyboard.readChar();
        if (s1.indexOf(c1)!=-1){
            String s2="";
            for (int i=0;i<s1.length();i++)
            if (s1.charAt(i)==c1)
            s2+="("+c1+")";
            else
            s2+=s1.charAt(i);
            System.out.println(s2);
        }
    }
}

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
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
Visit this user's website Find all posts by this user
Quote this message in a reply
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.
Visit this user's website Find all posts by this user
Quote this message in a reply
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;
uses wincrt;
var
s:string;
k:char;
i:integer;
begin
readln(s);
readln(k);
if pos(k,s)>0 then
  begin
   for i:=1 to length(s) do
     if s[i]=k then
       write('(',k,')')
     else
       write(s[i]);
  end
else
  writeln('Char is not in the string !');
end.
This code prints string directly on to screen if that char is inside of that string.
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 then
More about strings and functions with strings you have here :
http://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
Visit this user's website Find all posts by this user
Quote this message in a reply
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! Wink
python style
Code:
tempStr = raw_input("Enter a string: ");
tempChar = raw_input("Enter a charcter: ");
parsedStr = tempStr.replace(tempChar[0],'(' + tempChar[0] + ')');
print(parsedStr);

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;

public class Main {

    public static void main(String[] args) {
        String tempStr, charStr, finalStr;
        char mychar;

         //initialize scanner class to recieve input
        Scanner scan = new Scanner(System.in);
        
        //get string input
        System.out.println("Enter in a string");
        tempStr = scan.nextLine();
        
        //get character input
        System.out.println("Enter a character");
        charStr = scan.nextLine();
        
        //return first character of string input
        mychar = charStr.charAt(0);
        
        //replace occurances of character with brackets around it
        finalStr = tempStr.replaceAll(String.valueOf(mychar), '(' + String.valueOf(mychar) + ')');
        
        //print result
        System.out.println("Result: " + finalStr);

    }

}

Showing of the power of unix console Smile this will work on mac or linux Smile

Code:
echo Enter a string
read mystring

echo Enter a character
read mychar

echo $mystring | sed "s/$mychar/($mychar)/g"

I 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
Visit this user's website Find all posts by this user
Quote this message in a reply
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. Wink
Though I still don't understand this code, I tried it.
I'm using Python 2.5.

[Image: 20946_untitled.jpg]

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
11-26-2009, 08:54 AM
Post: #7
RE: [Java] Task 1
(11-26-2009 08:45 AM)Alphablend Wrote:  Wow Python is total ownage.
It seems so simple and yet so powerful. Wink
Though I still don't understand this code, I tried it.
I'm using Python 2.5.

[Image: 20946_untitled.jpg]

What part you don't understand and i'll explain.
Visit this user's website Find all posts by this user
Quote this message in a reply
11-26-2009, 08:57 AM
Post: #8
RE: [Java] Task 1
its about this line:
Code:
parsedStr = tempStr.replace(tempChar[0],'(' + tempChar[0] + ')')
i see that this is the main part of the program.
what this operation 'replace' does actually? and why do we have [0] in both of these places? Smile
hope this is not a nooby question, i'm a beginner in python and i just tried your code.

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
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.
Visit this user's website Find all posts by this user
Quote this message in a reply
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. Idea
i just had to turn on my brain for a sec.
ooh... i'm liking this language.Big Grin
thanks. Tongue

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: