Make Alternate Letters Capital

Make alternate letters capital

You are using i as both a loop variable (incrementing integer) and a variable to hold a string. That is why it isn't working. Try this fixed code of the function:

finaloutput = ''
i = 0
for e in word:
if i%2 == 0:
finaloutput = finaloutput + e.upper()
else:
finaloutput = finaloutput + e.lower()
i = i + 1
return finaloutput

You could also do a list comprehension:

''.join([e.lower() if c%2 else e.upper() for c,e in enumerate(a)])

How to make alternate characters in a string to uppercase?

In your program you were using replace() which replaces characters/CharSequences in the whole input what you need to do is

  • Put the string into an array.
  • Iterate over said array.
  • convert that array back into string

    private void genBTActionPerformed(java.awt.event.ActionEvent evt) {

    String str = new String(strTF.getText());
    char [] chr= str.toCharArray();
    int n = chr.length;
    char ch;
    int i;
    for(i = 0; i < n; i++) {
    if(i % 2 == 0) {
    ch = Character.toLowerCase(chr[i]);
    chr[i]=ch;
    } else {
    ch = Character.toUpperCase(chr[i]);
    chr[i]=ch;
    }
    }
    jumTF.setText(new String(chr)); }

hope this will help you :)

Convert alternate char to uppercase

First, java indexes start at 0 (not 1). I think you are asking for something as simple as alternating calls to Character.toLowerCase(char) and Character.toUpperCase(char) on the result of modulo (remainder of division) 2.

String x = jTextField1.getText();
for (int i = 0, len = x.length(); i < len; i++) {
char ch = x.charAt(i);
if (i % 2 == 0) {
System.out.print(Character.toLowerCase(ch));
} else {
System.out.print(Character.toUpperCase(ch));
}
}
System.out.println();

Alternate letters in upper case

scanf("%s, str) reads in a string until the first whitespace character. So when you type "tree gives us fruits" it reads in "tree" and then see's the whitespace and stops.

Try using fgets(str, 100, stdin) instead

https://www.cplusplus.com/reference/cstdio/fgets/

Alternating Capital Letters in Array Using PHP

By the way, you have a slight error in your capitalized string:

$string_1: a4nas60dj71wiena15sdl1131kg12b
$string_2: a4NaS60dJ71wIeNa15Sdl1131Kg12B
^ should be capital so out of sync for rest of string

I'll give you two ways of doing it:

<?php
header('Content-Type: text/plain');

$string_1 = "a4nas60dj71wiena15sdl1131kg12b";
$string_2 = "a4NaS60dJ71wIeNa15Sdl1131Kg12B";

$letter_count = 0;
$result = '';
for ($i=0; $i<strlen($string_1); $i++) {
if (!preg_match('![a-zA-Z]!', $string_1[$i])) {
$result .= $string_1[$i];
} else if ($letter_count++ & 1) {
$result .= strtoupper($string_1[$i]);
} else {
$result .= $string_1[$i];
}
}

$result2 = preg_replace_callback('!([a-zA-Z]\d*)([a-zA-Z])!', 'convert_to_upper', $string_1);

function convert_to_upper($matches) {
return strtolower($matches[1]) . strtoupper($matches[2]);
}

echo "$string_1\n";
echo "$string_2\n";
echo "$result\n";
echo "$result2\n";
?>

Note: The above makes several assumptions:

  1. Characters other than numbers and letters can be in the string;
  2. You want to alternate case regardless of the original (eg "ASDF" becomes "aSdF");
  3. You're capitalizing every second letter, not every second lowercase letter.

The above can be altered if these assumptions are incorrect.

How to alternate the case of a string

Here's a quick function to do it. It makes the entire string lowercase and then iterates through the string with a step of 2 to make every other character uppercase.

var alternateCase = function (s) {
var chars = s.toLowerCase().split("");
for (var i = 0; i < chars.length; i += 2) {
chars[i] = chars[i].toUpperCase();
}
return chars.join("");
};

var txt = "hello world";
console.log(alternateCase(txt));

HeLlO WoRlD

The reason it converts the string to an array is to make the individual characters easier to manipulate (i.e. no need for String.prototype.concat()).

How to capitalize each alternate character of a string?

This should solve your function, the hard part is just checking weather the character is letters or not, using inout and replace range would give better performance:

func altCaptalized(string: String) -> String {
var stringAr = string.characters.map({ String($0) }) // Convert string to characters array and mapped it to become array of single letter strings
var numOfLetters = 0

// Convert string to array of unicode scalar character to compare in CharacterSet
for (i,uni) in string.unicodeScalars.enumerated() {
//Check if the scalar character is in letter character set
if CharacterSet.letters.contains(uni) {
if numOfLetters % 2 == 0 {
stringAr[i] = stringAr[i].uppercased() //Replace lowercased letter with uppercased
}
numOfLetters += 1
}
}

return stringAr.joined() //Combine all the single letter strings in the array into one string
}


Related Topics



Leave a reply



Submit