0% found this document useful (0 votes)
7 views2 pages

Program

The document contains two versions of a Java program that modifies sentences by adding the article 'an' before words that start with a vowel. The first version handles capitalization differently, while the second version uses a StringBuilder for constructing the new sentence. Both programs prompt the user for input and output the modified sentence accordingly.

Uploaded by

rohitsingh57395
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Program

The document contains two versions of a Java program that modifies sentences by adding the article 'an' before words that start with a vowel. The first version handles capitalization differently, while the second version uses a StringBuilder for constructing the new sentence. Both programs prompt the user for input and output the modified sentence accordingly.

Uploaded by

rohitsingh57395
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

```

import java.util.Scanner;

public class AddAnArticle {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a sentence: ");


String sentence = scanner.nextLine();

String[] words = sentence.split("\\s+");

for (int i = 0; i < words.length; i++) {


if (isVowel(words[i].charAt(0))) {
words[i] = "an " + words[i];
} else if (isCapitalVowel(words[i].charAt(0))) {
words[i] = "An " + words[i].substring(1);
}
}

String newSentence = String.join(" ", words);

System.out.println("New Sentence: " + newSentence);

scanner.close();
}

public static boolean isVowel(char c) {


String vowels = "aeiou";
return vowels.indexOf(Character.toLowerCase(c)) != -1;
}

public static boolean isCapitalVowel(char c) {


String vowels = "AEIOU";
return vowels.indexOf(c) != -1;
}
}
```

```

```
import java.util.Scanner;

public class AddAnArticle {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a sentence: ");


String sentence = scanner.nextLine();

String[] words = sentence.split("\\s+");

StringBuilder newSentence = new StringBuilder();

for (int i = 0; i < words.length; i++) {


if ("aeiouAEIOU".indexOf(words[i].charAt(0)) != -1) {
if (i == 0) {
newSentence.append("An ").append(words[i]);
} else {
newSentence.append(" an ").append(words[i]);
}
} else {
newSentence.append(" ").append(words[i]);
}
}

System.out.println("New Sentence: " + newSentence.toString().trim());

scanner.close();
}
}
```

You might also like