字符串大写转小写库函数_PHP程序无需使用库函数即可将字符串转换为大写

字符串大写转小写库函数

Given a string and we have to convert it into uppercase string without using any library function.

给定一个字符串,我们必须在不使用任何库函数的情况下将其转换为大写字符串。

PHP code:

PHP代码:

<?php
//function definition
//this function accepts a string/text, converts
//text to uppercase and return the uppercase converted string
function upperCase($str)
{
$chars  = str_split($str);
$result = '';
//loop from 0th character to the last character
for ($i = 0; $i < count($chars); $i++) {
//extracting the character and getting its ASCII value
$ch = ord($chars[$i]);
//if character is a lowercase alphabet then converting 
//it into an uppercase alphabet
if ($chars[$i] >= 'a' && $chars[$i] <= 'z')
$result .= chr($ch - 32);
else
$result .= $chars[$i];
}
//finally, returning the string
return $result;
}
//function calling
$text = "hello world";
echo upperCase($text);
echo "<br>";
$text = "Hello world!";
echo upperCase($text);
echo "<br>";
$text = "[email protected]";
echo upperCase($text);
echo "<br>";
?>

Output

输出量

HELLO WORLD
HELLO WORLD!
[email protected]

Code explanation:

代码说明:

We convert the string ($str) into an array of characters ($chars) then calculate their ASCII value using ord() function. Since we know that in ASCII, the Upper Case characters come exactly 32 places before the lower case equivalent, we subtract 32 from the ASCII value and then convert it back to the character using the chr() function. The output is stored in the $result variable.

我们将字符串( $ str )转换为字符数组( $ chars ),然后使用ord()函数计算其ASCII值 。 由于我们知道在ASCII中,大写字符恰好在小写字母之前32位,因此我们从ASCII值减去32,然后使用chr()函数将其转换回字符 。 输出存储在$ result变量中。

This program is a good proof of concept.

该程序是概念的很好证明。

翻译自: https://www.includehelp.com/php/convert-string-to-uppercase-without-using-the-library-function.aspx

字符串大写转小写库函数

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/546076.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

zoj 1091 Knight Moves

题目见zoj 1091 使用宽度搜索优先来求解&#xff0c;这个算法已经忘记的差不多了&#xff0c;所以写出来的代码很罗嗦&#xff0c;看起来很不清晰。 好像还可以直接用公式或者神经网络算法求解,详见Knights Tour /* zoj 1091 Knight Moves */ #include <stdio.h> #incl…

1.基本类型的指针

例子一&#xff1a; #include<stdio.h>int main() {int i10;int * p&i; //p是个变量名字&#xff0c;int * 表示该p变量只能存储int类型变量的地址。//int *p&i;等价于 int *p; p&i;//p存放了i的地址,p指向i,*p就是i变量本身printf("%d\n",p);// …

VB中KeyCode常数用法 VB 按键

VB中KeyCode常数用法可在代码中的任何地方用下列常数代替实际值&#xff1a;常数 值 描述vbKeyLButton 0x1 鼠标左键vbKeyRButton 0x2 鼠标右键vbKeyCancel 0x3 CANCEL 键vbKeyMButton 0x4 鼠标中键vbKeyBack 0x8 BACKSPACE 键vbKeyTab 0x9 TAB 键vbKeyClear 0xC CLEAR 键vbKey…

zoj 1088 System Overload

约瑟夫环 (josephus problem &#xff09;问题&#xff0c;有公式 可以直接套用 我使用暴力破解方法求解&#xff08;用时3秒多&#xff09;。 代码如下&#xff1a; /* zoj 1088 System Overload */ #include <stdio.h> #include <string.h>#define MAXBUILDING …

链表竟然比数组慢了1000多倍?(动图+性能评测)

这是我的第 215 期分享作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;数组和链表是程序中常用的两种数据结构&#xff0c;也是面试中常考的面试题之一。然而对于很多人来说…

c语言用宏定义常量_使用宏定义常量以在C的数组声明中使用

c语言用宏定义常量As we know that, while declaring an array we need to pass maximum number of elements, for example, if you want to declare an array for 10 elements. You need to pass 10 while declaring. Example: int arr[10]; 众所周知&#xff0c;在声明数组时…

HTML字体颜色设置

html字体颜色设置behavior移动效果>插入文字,alternate(交替滚动)、slide(幻灯片效果,指的是滚动一次,然后停止滚动) scrollAmount"移动速度" direction"方向"字体颜色设置字体< f o n t f a c e 宋 体 c o l o r # 9 9 3 2 c c s i z e 5 > 字…

zoj 1025 Wooden Sticks

题目见&#xff1a;zoj 1025 先对木棒按照长度进行排序&#xff0c;然后再计算对应重量构成的数组中非递减子序列的个数。 相关代码&#xff08;悲催的是该代码不能在poj1065 和hdoj1051 下通过&#xff0c;懒得看具体是什么原因了&#xff09; /* zoj 1025 Wooden sticks *…

win7安装python

2019独角兽企业重金招聘Python工程师标准>>> 官网下载 python 安装包&#xff0c;一个 .msi 文件&#xff0c;双击安装 右键[计算机]-属性-高级系统设置-环境变量&#xff0c;在[系统变量]中找到 Path 变量&#xff0c;双击&#xff0c;在值后面添加 python 的安装路…

漫画:Java如何实现热更新?

Arthas&#xff08;阿尔萨斯&#xff09;是 Alibaba 开源的一款 Java 诊断工具&#xff0c;使用它我们可以监控和排查 Java 程序&#xff0c;然而它还提供了非常实用的 Java 热更新功能。所谓的 Java 热更新是指在不重启项目的情况下实现代码的更新与替换。使用它可以实现不停机…

十进制数转换为十六进制数_十进制数制到十六进制数制的转换

十进制数转换为十六进制数Conversion of decimal number system into hexadecimal number system can be done by successively dividing an integral part by 16 till the quotient is 0 and then reading the remainder of all in the bottom to the top manner, where the b…

编译器开发相关资源

开发编译器相关的一些网络资源&#xff1a; how difficult is it to write a compiler? wikipedia compiler compiler 101 code competetion pl/0 (couses &#xff0c;source code ,another source code )and pl/I language the gentle compiler construction sys…

计算机二级考试C++考试大纲

基本要求: 1. 掌握C语言的基本语法规则。 2. 熟练掌握有关类与对象的相关知识。 3. 能够阅读和分析C程序。 4. 能够采用面向对象的编程思路和方法编写应用程序。 5. 能熟练使用Visual C6.0集成开发环境编写和调度程序。 考试内容&#xff1a; 一、 C语言概述 1. 了解C语言的基本…

自动化运维工具Saltstack详细介绍

Saltstack是一个新的基础设施管理工具。目前处于快速发展阶段&#xff0c;可以看做是pssh弱化的Puppet的组合。间接的反映出了saltstack的两大功能&#xff1a;远程执行和配置管理。Saltstack使用Python开发&#xff0c;是一个非常简单易用和轻量级的管理工具。由Master和Minio…

为什么建议你使用枚举?

枚举是 JDK 1.5 新增的数据类型&#xff0c;使用枚举我们可以很好的描述一些特定的业务场景&#xff0c;比如一年中的春、夏、秋、冬&#xff0c;还有每周的周一到周天&#xff0c;还有各种颜色&#xff0c;以及可以用它来描述一些状态信息&#xff0c;比如错误码等。枚举类型不…

数据结构树二叉树计算节点_查找二叉树中叶节点的数量 数据结构

数据结构树二叉树计算节点Algorithm: 算法&#xff1a; One of the popular traversal techniques to solve this kind of problems is level order tree traversal (Read: Level Order Traversal on a Binary Tree) where we use the concept of BFS. 解决此类问题的一种流行…

重磅!阿里推出国产开源JDK!

简介Alibaba Dragonwell 是一款免费的, 生产就绪型Open JDK 发行版&#xff0c;提供长期支持&#xff0c;包括性能增强和安全修复。阿里巴巴拥有最丰富的Java应用场景&#xff0c;覆盖电商&#xff0c;金融&#xff0c;物流等众多领域&#xff0c;世界上最大的Java用户之一。Al…

部分排序算法c语言实现

代码比较粗糙&#xff0c;主要是用于对排序算法的理解&#xff0c;因而忽略了边界和容错处理相关代码。 相关文档&#xff1a; Insert Sort ,Bubble Sort ,Select Sort ,Shell sort ,Quick sort ,Heap sort ,Merge sort on Wikipedia algorithm Repository :C语言实现部分排…

计算机等级考试二级ACCESS考试大纲

公共基础知识部分30分 专业语言部分 70分 基本要求 1. 具有数据库系统的基础知识。 2. 基本了解面各对象的概念。 3. 掌握关系数据库的基本原理。 4. 掌握数据库程序设计方法。 5. 能使用Access建立一个小型数据库应用系统。 考试内容 一、 数据库基础知识 1. 基本概念&#xf…