English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

1) Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.

2) Write a program that converts all lower case characters to upper case, all upper case characters to lower case and disregards everything else except for “blanks” and “new lines”.

2006-09-04 23:14:52 · 7 answers · asked by sham230 1 in Computers & Internet Programming & Design

7 answers

Since you asked the question over two months ago, and it can't be important, for the assignment that is, I will try to give the answer.

If you want it in 2 languages, then I may have to give you the psuedo code, so you can understand how it works.

It should not matter how you get the input and output files, just knowing that you can read one and write to the other is good enough. There are several ways of getting the files, you can include them as OS parameters on the program you are starting, like java MyProgram (then use the > and < operators to add the files for input and output. I cannot remember at this time which is which, but I think > is for in and < is for out) > infile.txt < outfile.txt
Alternatively, you could accept the filenames as input arguments to the program.

Anyhow, what you want to do after getting the files is:

1)
- Read in a line from the input file.
- If the line is empty (has no character except a return or new line)
--- Print a return (new line) to the output file.
- Else
--- Set a flag (boolean) to signal if the last character was a space
--- Get the first character in the line
--- (We will call this Section 1, as we will need to loop back here)
--- If the character is not a space
----- Print the character out (to the output file)
----- Reset the flag to signal last character was a space
--- Else If the flag to signal last character is not set
----- Print one space out
----- Set the flag to signal the last character was a space
--- Else
----- Skip the character
--- (End if)
--- Get the next character in the line
--- If no more characters left (End of Line)
----- Print out a return (new line)
----- Get the next line in the file
----- If no more lines in file
------- End of program
--- (End if)
--- (Go to Section 1 and repreat with the new character)
- (End if)

It is basic, has a minor efficiency problem, hopefully you can work those issues out if you need to.

2)
- Go through the same iteration process as above, ie get a line, get a character, check the character, get next character or line, etc.
- Check if the character is a letter. Java have some helper methods, ie, in String you could use the method toUpperCase() or toLowerCase() to get the whole line changed. In C++ you would probably have to check if the character is in the lowercase range or uppercase range and change the value of the character (it is just a number after all) to get the uppercase or lowercase version. It would be wise to check out an ASCII table to see the numbers for these characters (upper and lowercase). One place for ASCII is here http://www.lookuptables.com/

Thats about it.

2006-09-06 14:45:37 · answer #1 · answered by Mark aka jack573 7 · 0 0

Cant do that buddy.Im not gonna write code for you. These are simple programs which require just a little thought. The upper case lower case pgms have functions you could Its simple actually, not a big pgm. Just think how you could do it.

2006-09-05 06:31:32 · answer #2 · answered by rosha85 2 · 0 1

Do homework!

1.
// I won't do input for u; it is complicated in Java, assume that
String input = "something... ";
// study regular expressions if you don't get it!
String output = input.replaceAll("\s+", " ");
System.out.println(output);

2.
String text = "...";
StringBuffer mod = new StringBuffer(text);
for (int x = 0; x < text.length; x++) {
char c = mod.charAt(x);
if (c >= 'A' && c <= 'Z') mod.setCharAt(mod.charAt(x) - 'A' + 'a');
if (c >= 'a' && c <= 'z') mod.setCharAt(mod.charAt(x) - 'a' + 'A');
}

If you do read all that, 80% for you on assignment, but I've seen too many lazy bums to reserve judgement.

2006-09-05 07:18:17 · answer #3 · answered by Andy T 7 · 0 2

I //this is program to convert uppercase to lowercase and vice versa

//soultion for the problem 2

// C language
#include
#include

void main()
{
char str[50];
int len;
int i;
clrscr();
printf("Enter the String");
scanf("%s",str);
len=strlen(str);
for(i=0;i {
if(str[i]>=65 && str[i]<=90)
str[i]+=32;
else if(str[i]>=97 && str[i]<=112)
str[i]-=32;

}
printf("Output String is:%s",str);
}


//soultion for the problem 1

// C language
#include
#include

void main()
{
char str[50];
char str1[50];
int len;
int i,j=0;
clrscr();
printf("Enter the String");
gets(str);
len=strlen(str);
for(i=0;i {

if(str[i]==32 && str[i+1]==32)
str1[j]=32
else
str1[j]=str[i];
j++;

}
str1[j]='\0';
printf("Output String is:%s",str1);
}

2006-09-05 06:29:55 · answer #4 · answered by allen ravi 2 · 0 1

Try solving it using perl first.

2006-09-05 06:20:38 · answer #5 · answered by JavaClark 5 · 0 1

Go do your own homework.

2006-09-05 06:20:15 · answer #6 · answered by rissaofthesaiyajin 3 · 1 1

Why ?

2006-09-05 06:19:14 · answer #7 · answered by Anonymous · 0 1

fedest.com, questions and answers