How to fill a char array in C
Like this, probably
size_t prevlen = strlen(tval);
memset(tval + prevlen, ' ', 19 - prevlen);
*(tval + 19) = '\0';
Filling a character array with characters from a string
using ToCharArray()
strings can be converted into character arrays.
Console.WriteLine("Enter a string: ");
var name = Console.ReadLine();
var intoarray= name.ToCharArray();
foreach (var n in intoarray)
Console.WriteLine(n);
if you are using foreach, you should wait for the index to behave as if you were taking the value.
Console.WriteLine(n);
how to fill a char array with a given string
char[] mSeqA=parts[0].toCharArray();
char[] mSeqB=parts[1].toCharArray();
Filling a character array with subsequent letters in a string
Just put the chars to the array in a loop. You can use the original string's length to put in the fitting char using charAt(i % a.length())
.
String a = "Example";
char[] b = new char[10];
for (int i = 0; i < b.length; i++) {
b[i] = a.charAt(i % a.length());
}
for (char x : b) System.out.println(x);
Is there a way to fill string with characters at some specific indices?
You can't use String
in the way you suggest, because String
is immutable.
Another option, instead of StringBuilder
, is to use a raw array of char
. You know that the resultant string will have the same length as the source string, so you can do it like this:
- Create an array using String.toCharArray.
- Iterate from both ends simultaneously using indices
i
andj
. Start indexi
from 0 moving forwards, and j from array length - 1, moving backwards. - Check the elements at i and j for a letter using Character.isLetter. If it's not a letter, just skip it by advancing i forwards or j backwards.
- Swap the array elements at i and j.
- Repeat while i < j.
- Create the new string using
new String(charArray)
As this looks like an exercise, I'll leave the actual coding to you.
Caveat: the above only works for strings where the Unicode code points are in the Basic Multilingual Plane (i.e. most strings to you and me). It won't work for tricky situations containing certain characters such as smileys (for more info on this potential problem, see https://dzone.com/articles/the-right-way-to-reverse-a-string-in-java).
fill array with character in string
You're assigning a character to a position in a String array. In your code, arr[i] refers to a String, and s.charAt(x) is a char.
It seems like arr should be a char array instead of a String array.
how to fill a char array with DIFFRENT chars in java?
The Answer by YourHelper seems correct.
Avoidchar
Unfortunately, even the fixed version of your code will fail with most characters. The char
type is essentially broken since Java 2, and legacy since Java 5.
To see char
break, use a character like “”.
Use code point integers instead.
StringBuilder
as resultSomething like this untested code.
int limit = 5 ;
StringBuilder sb = new StringBuilder() ;
Scanner sc = new Scanner(System.in);
String input;
int codePoint;
for ( int i = 0 ; i < limit ; i++ ) {
input = sc.next();
int[] codePoints = input.codePoints().toArray() ;
if ( codePoints.length == 1 ) {
codePoint = codePoints[ 0 ] ;
System.out.println( "for position " + i + " the input character is: " + Character.toString( codePoint ) );
sb.appendCodePoint( codePoint );
System.out.println("so we get the current result " + sb.toString() );
} else {
System.out.println( "Done: " + sb.toString() ) ;
break;
}
}
System.out.println( "Done: " + sb.toString() ) ;
See this code run live at IdeOne.com.
for index 0 the input character is: H
so we get the current result H
for index 1 the input character is: br>so we get the current result Hbr>for index 2 the input character is: l
so we get the current result Hl
for index 3 the input character is: l
so we get the current result Hll
for index 4 the input character is: o
so we get the current result Hllo
Done: Hllo
List of strings (characters) as resultIf you want to collect each character individually, make a list of strings. Make a string of each code point, and add to list. Omit the StringBuilder
.
List< String > characters = new ArrayList <>() ;
…
characters.add( Character.toString( codePoint ) ) ;
See an example of this code run live at IdeOne.com.
for index 0 the input character is: H
so we get the current result [H]
for index 1 the input character is: br>so we get the current result [H, ]
for index 2 the input character is: l
so we get the current result [H, , l]
for index 3 the input character is: l
so we get the current result [H, , l, l]
for index 4 the input character is: o
so we get the current result [H, , l, l, o]
Done: [H, , l, l, o]
Be aware that in some languages what you may perceive as one "letter" is actually a combination of two characters, a letter followed by a "combining diacritical". Example here. Discussion here.
Related Topics
C#, Access Child Properties from Parent Reference
How to Get Nested Json Values in C#
Cast Class into Another Class or Convert Class to Another
Select Distinct by Two Properties in a List
How to Remove Illegal Characters from Path and Filenames
First Call to Web API Is Very Slow C#
Securely Store a Password in the Application in C#
Split String and Keep Delimiter in Sequence
Using Linq to Groupby and Sum Datatable
How to Format a String as a Telephone Number in C#
How to Get Only Date from a Datetime Value in Razor Page
Error: the Entity Type Requires a Primary Key
Asp.Net Core API Post Parameter Is Always Null
How to Check If a Datetime Value Is Empty or Not in a Put Request
Smtpexception: Unable to Read Data from the Transport Connection: Net_Io_Connectionclosed