PocketPixie
PocketPixie
CC#
Created by PocketPixie on 10/19/2024 in #help
Where is the letter of the array saved?
Hello everyone, Just a quick question. I understand this code based upon the comments but I am wondering why ans just being incremented has anything to do with the logic in comparing where index i is in the string?
public class Program {
public static int CountDs(string str)
{ //Keeping count of the answer with ans
int ans = 0;
//Creating a for-loop to iterate over the string
for(int i = 0; i < str.Length; i++)
{
//If at the str at index i either equals d or D, increment ans by 1
if(str[i] == 'd' || str[i] == 'D')
{
ans++;
}
}
return ans;
}
}
public class Program {
public static int CountDs(string str)
{ //Keeping count of the answer with ans
int ans = 0;
//Creating a for-loop to iterate over the string
for(int i = 0; i < str.Length; i++)
{
//If at the str at index i either equals d or D, increment ans by 1
if(str[i] == 'd' || str[i] == 'D')
{
ans++;
}
}
return ans;
}
}
How does the program know how many D's are in the string if it just increments after coming across a D or a d? Where does it 'keep' the d or D?
7 replies
CC#
Created by PocketPixie on 11/27/2023 in #help
Help with this coding problem.
Hello all, can someone diagnois what does it mean by this code stack?
public class Solution {
public int[] SortArrayByParityII(int[] nums) {
int n = Convert.ToInt32(nums);
if(n % 2 == 0)
{
Console.WriteLine(n);
}
else
{
Console.WriteLine(n);
}
}
}
public class Solution {
public int[] SortArrayByParityII(int[] nums) {
int n = Convert.ToInt32(nums);
if(n % 2 == 0)
{
Console.WriteLine(n);
}
else
{
Console.WriteLine(n);
}
}
}
The problem is
Line 2: Char 18: error CS0161: 'Solution.SortArrayByParityII(int[])': not all code paths return a value (in Solution.cs)
Line 2: Char 18: error CS0161: 'Solution.SortArrayByParityII(int[])': not all code paths return a value (in Solution.cs)
I've tried converting the array in the method to an int type but that doesn't work
15 replies