Easy

Find the leftmost digit that occurs in a given string.

Example

  • For inputString = "var_1__Int", the output should be
    firstDigit(inputString) = '1';
  • For inputString = "q2q-q", the output should be
    firstDigit(inputString) = '2';
  • For inputString = "0ss", the output should be
    firstDigit(inputString) = '0'.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    A string containing at least one digit.

    Guaranteed constraints:
    3 ≤ inputString.length ≤ 10.

  • [output] char

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

char firstDigit(char *inputString)
{
	for(int i=0;i<strlen(inputString);i++)
		if(isdigit(inputString[i]))
			return inputString[i];
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> arrayMaxConsecutiveSum  (0) 2020.04.05
<Codesignal> differentSymbolsNaive  (0) 2020.04.05
<Codesignal> extractEachKth  (0) 2020.04.05
<Codesignal> stringsRearrangement  (0) 2020.04.05
<Codesignal> absoluteValuesSumMinimization  (0) 2020.04.05

+ Recent posts