Verifique si los caracteres en mayúsculas en una string se usan correctamente o no

Dada una string S que consta de letras mayúsculas y minúsculas, la tarea es verificar si los caracteres en mayúscula se usan correctamente en la string dada o no. El uso correcto de los caracteres en mayúsculas es el siguiente:

  • Todos los caracteres de la string están en mayúsculas. Por ejemplo, «GEEKS» .
  • Ninguno de los caracteres está en mayúsculas. Por ejemplo, «geeks» .
  • Solo el primer carácter está en mayúsculas. Por ejemplo, «Geeks» .

Ejemplos:

Entrada: S = «Geeks»
Salida:
Explicación:  Solo el primer carácter de la string está en mayúsculas y todos los caracteres restantes están en minúsculas.

Entrada: S = «GeeksForGeeks»
Salida: No

Enfoque: siga los pasos a continuación para resolver el problema:

  • Compruebe si el primer carácter de la string está en mayúsculas o no . Si se encuentra que es cierto, itere sobre los caracteres restantes.
  • Si todos los caracteres restantes están en mayúsculas, escriba «Sí» . De lo contrario, si alguno de los caracteres restantes está en mayúsculas, imprima «NO» .
  • Si el primer carácter no está en mayúsculas, compruebe si todos los caracteres restantes están en minúsculas o no. Si se encuentra que es cierto, escriba «SÍ» . De lo contrario, escriba “NO” .

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the
// character c is in lowercase or not
bool isLower(char c)
{
 
    return c >= 'a' and c <= 'z';
}
 
// Function to check if the
// character c is in uppercase or not
bool isUpper(char c)
{
 
    return c >= 'A' and c <= 'Z';
}
 
// Utility function to check if uppercase
// characters are used correctly or not
bool detectUppercaseUseUtil(string S)
{
   
    // Length of string
    int N = S.size();
    int i;
 
    // If the first character is in lowercase
    if (isLower(S[0]))
    {
        i = 1;
        while (S[i] && isLower(S[i]))
            ++i;
        return i == N ? true : false;
    }
 
    // Otherwise
    else {
        i = 1;
 
        // Check if all characters
        // are in uppercase or not
        while (S[i] && isUpper(S[i]))
            ++i;
 
        // If all characters are
        // in uppercase
        if (i == N)
            return true;
        else if (i > 1)
            return false;
 
        // Check if all characters except
        // the first are in lowercase
        while (S[i] && isLower(S[i]))
            ++i;
        return i == N ? true : false;
    }
}
 
// Function to check if uppercase
// characters are used correctly or not
void detectUppercaseUse(string S)
{
   
    // Stores whether the use of uppercase
    // characters are correct or not
    bool check = detectUppercaseUseUtil(S);
 
    // If correct
    if (check)
        cout << "Yes";
 
    // Otherwise
    else
        cout << "No";
}
 
// Driver Code
int main()
{
   
    // Given string
    string S = "GeeKs";
 
    // Function call to check if use of
    // uppercase characters is correct or not
    detectUppercaseUse(S);
 
    return 0;
}

Java

/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
 
  // Function to check if the
  // character c is in lowercase or not
  static boolean isLower(char c)
  {
 
    return c >= 'a' && c <= 'z';
  }
 
  // Function to check if the
  // character c is in uppercase or not
  static boolean isUpper(char c)
  {
 
    return c >= 'A' && c <= 'Z';
  }
 
  // Utility function to check if uppercase
  // characters are used correctly or not
  static boolean detectUppercaseUseUtil(String S)
  {
    // Length of string
    int N = S.length();
 
    int i;
 
    // If the first character is in lowercase
    if (isLower(S.charAt(0))) {
      i = 1;
      while (i<N && isLower(S.charAt(i)))
        ++i;
      return i == N ? true : false;
    }
 
    // Otherwise
    else {
      i = 1;
 
      // Check if all characters
      // are in uppercase or not
      while (i<N && isUpper(S.charAt(i)))
        ++i;
 
      // If all characters are
      // in uppercase
      if (i == N)
        return true;
      else if (i > 1)
        return false;
 
      // Check if all characters except
      // the first are in lowercase
      while (i<N && isLower(S.charAt(i)))
        ++i;
      return i == N ? true : false;
    }
  }
 
  // Function to check if uppercase
  // characters are used correctly or not
  static void detectUppercaseUse(String S)
  {
     
    // Stores whether the use of uppercase
    // characters are correct or not
    boolean check = detectUppercaseUseUtil(S);
 
    // If correct
    if (check)
      System.out.println("Yes");
 
    // Otherwise
    else
      System.out.println("No");
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    // Given string
    String S = "GeeKs";
 
    // Function call to check if use of
    // uppercase characters is correct or not
    detectUppercaseUse(S);
  }
}
 
// This code is contributed by subhamsingh10

Python3

# Python3 program for the above approach
 
# Function to check if the
# character c is in lowercase or not
def isLower(c):
    return ord(c) >= ord('a') and ord(c) <= ord('z')
 
# Function to check if the
# character c is in uppercase or not
def isUpper(c):
    return ord(c) >= ord('A') and ord(c) <= ord('Z')
 
# Utility function to check if uppercase
# characters are used correctly or not
def detectUppercaseUseUtil(S):
   
    # Length of string
    N = len(S)
    i = 0
 
    # If the first character is in lowercase
    if (isLower(S[0])):
        i = 1
        while (S[i] and isLower(S[i])):
            i += 1
        return True if (i == N) else False
 
    # Otherwise
    else:
        i = 1
 
        # Check if all characters
        # are in uppercase or not
        while (S[i] and isUpper(S[i])):
            i += 1
 
        # If all characters are
        # in uppercase
        if (i == N):
            return True
        elif (i > 1):
            return False
 
        # Check if all characters except
        # the first are in lowercase
        while (S[i] and isLower(S[i])):
            i += 1
        return True if (i == N) else False
 
# Function to check if uppercase
# characters are used correctly or not
def detectUppercaseUse(S):
   
    # Stores whether the use of uppercase
    # characters are correct or not
    check = detectUppercaseUseUtil(S)
 
    # If correct
    if (check):
        print("Yes")
    # Otherwise
    else:
        print ("No")
 
# Driver Code
if __name__ == '__main__':
   
    # Given string
    S = "GeeKs"
 
    # Function call to check if use of
    # uppercase characters is correct or not
    detectUppercaseUse(S)
 
# This code is contributed by mohit kumar 29.

C#

using System;
public class GFG
{
 
  // Function to check if the
  // character c is in lowercase or not
  static bool isLower(char c)
  {
 
    return c >= 'a' && c <= 'z';
  }
 
  // Function to check if the
  // character c is in uppercase or not
  static bool isUpper(char c)
  {
 
    return c >= 'A' && c <= 'Z';
  }
 
  // Utility function to check if uppercase
  // characters are used correctly or not
  static bool detectUppercaseUseUtil(string S)
  {
 
    // Length of string
    int N = S.Length;
 
    int i;
 
    // If the first character is in lowercase
    if (isLower(S[0]))
    {
      i = 1;
      while (i < N && isLower(S[i]))
        ++i;
      return i == N ? true : false;
    }
 
    // Otherwise
    else {
      i = 1;
 
      // Check if all characters
      // are in uppercase or not
      while (i < N && isUpper(S[i]))
        ++i;
 
      // If all characters are
      // in uppercase
      if (i == N)
        return true;
      else if (i > 1)
        return false;
 
      // Check if all characters except
      // the first are in lowercase
      while (i < N && isLower(S[i]))
        ++i;
      return i == N ? true : false;
    }
  }
 
  // Function to check if uppercase
  // characters are used correctly or not
  static void detectUppercaseUse(string S)
  {
 
    // Stores whether the use of uppercase
    // characters are correct or not
    bool check = detectUppercaseUseUtil(S);
 
    // If correct
    if (check)
      Console.WriteLine("Yes");
 
    // Otherwise
    else
      Console.WriteLine("No");
  }
 
  // Driver Code
  static public void Main ()
  {
     
    // Given string
    string S = "GeeKs";
 
    // Function call to check if use of
    // uppercase characters is correct or not
    detectUppercaseUse(S);
  }
}
 
// This code is contributed by avanitrachhadiya2155

Javascript

<script>
      // JavaScript program for the above approach
      // Function to check if the
      // character c is in lowercase or not
      function isLower(str) {
        return str === str.toLowerCase();
      }
 
      // Function to check if the
      // character c is in uppercase or not
      function isUpper(str) {
        return str === str.toUpperCase();
      }
 
      // Utility function to check if uppercase
      // characters are used correctly or not
      function detectUppercaseUseUtil(S) {
        // Length of string
        var N = S.length;
        var i;
 
        // If the first character is in lowercase
        if (isLower(S[0])) {
          i = 1;
          while (S[i] && isLower(S[i])) ++i;
          return i === N ? true : false;
        }
 
        // Otherwise
        else {
          i = 1;
 
          // Check if all characters
          // are in uppercase or not
          while (S[i] && isUpper(S[i])) ++i;
 
          // If all characters are
          // in uppercase
          if (i === N) return true;
          else if (i > 1) return false;
 
          // Check if all characters except
          // the first are in lowercase
          while (S[i] && isLower(S[i])) ++i;
          return i === N ? true : false;
        }
      }
 
      // Function to check if uppercase
      // characters are used correctly or not
      function detectUppercaseUse(S) {
        // Stores whether the use of uppercase
        // characters are correct or not
        var check = detectUppercaseUseUtil(S);
 
        // If correct
        if (check) document.write("Yes");
        // Otherwise
        else document.write("No");
      }
 
      // Driver Code
      // Given string
      var S = "GeeKs";
      // Function call to check if use of
      // uppercase characters is correct or not
      detectUppercaseUse(S);
    </script>
Producción: 

No

 

Complejidad temporal: O(N)
Espacio auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por swagami6 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *