假设你有:
const char * something = "m";
Run Code Online (Sandbox Code Playgroud)
如何使用toupper(或其他东西,如果适用)制作这个大写字母?
我想用一个char *而不是一个string(我可以使用一个字符串,但我必须使用str.c_str()).
那么,如何才能让char * something = "m";含有"M"?
所以,我正在尝试编写一个接受字符串并将所有小写值更改为大写的函数.这是代码:
void lowerToUpper(char *s)
{
char *p;
for (p = s; *p; p++)
{
if (islower(*p))
*p = toupper(*p);
}
}
int main (int argc, char * argv[])
{
char *pa;
pa = "This is a test.";
printf("The following string will be edited:\n");
printf("%s\n%s\n%s\n", pa);
lowerToUpper(pa);
printf("The string has been edited, and is now as follows:\n");
printf("%s\n%s\n%s", pa);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
问题来自"*p = toupper(*p);"这一行,这是我得到分段错误的地方.我的猜测是问题出现在尝试分配toupper(*p)返回*p的值.在做了一些测试之后,似乎toupper(*p)工作,但是一旦我尝试将值赋给*p,我就会出错?有关为什么会发生这种情况的任何想法?
有人可以解释为什么C++中的这个短代码不会产生预期的输出.代码应该用大写字母打印字符串.
#include <iostream>
#include <string>
using namespace std;
int main(){
string sample("hi, i like cats and dogs.");
cout << "small: " << sample << endl << "BIG : ";
for(char c: sample)
cout << toupper(c);
cout<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上述程序的输出是:
small: hi, i like cats and dogs.
BIG : 72734432733276737569326765848332657868326879718346
Run Code Online (Sandbox Code Playgroud)
但我期待:
small: hi, i like cats and dogs.
BIG : HI, I LIKE CATS AND DOGS.
Run Code Online (Sandbox Code Playgroud)
我只用python编程.
我正在尝试将位于第一个空格之后的字符串中的第一个字母大写:
string name = "Jeffrey steinberg";
Run Code Online (Sandbox Code Playgroud)
我正试图在斯坦伯格大写S.我不知道该怎么做.我已经尝试使用toupper函数搞乱,但不知道如何引用字符"s",因为c#字符串不是像c中那样的数组.
如何在C语言中使用topper和tolower?我已经尝试运行我已经制作的程序,它运行正常问题是因为我应该将它提交到网站检查它是对还是错,每次我提交它,它说编译错误.
我在macbook上制作代码,使用Xcode,它在我的toupper和tolower代码上说 - 函数'toupper'的隐式声明在C99中无效
#include <stdio.h>
#include <string.h>
int main()
{
int input;
scanf("%d",&input);
int jumlahkata;
char kalimat[100];
for(int i=0;i<input;i++)
{
scanf("%s",kalimat);
jumlahkata=strlen(kalimat);
for(int j=0;j<jumlahkata;j++)
{
if(j%2==0 || j==0)
{
kalimat[j]=toupper(kalimat[j]);
}
else
{
kalimat[j]=tolower(kalimat[j]);
}
}
printf("%s\n",kalimat);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 所以这是我用来转换小写的程序,大写你可以告诉我为什么我们使用这个东西?[(str [i]> = 97 && str [i] <= 122)]在下面的代码部分?
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char str[20];
int i;
cout << "Enter the String (Enter First Name) : ";
cin >> str;
for (i = 0; i <= strlen(str); i++) {
if (str[i] >= 97 && str[i] <= 122) //Why do we use this???
{
str[i] = str[i] - 32;
}
}
cout << "\nThe String in Uppercase = " << str;
getch();
}
Run Code Online (Sandbox Code Playgroud) 我一直在搜索和搜索,但没有找到这个问题的答案.我要求用户输入他们的名字,我想自动格式化名称,这样无论他们如何输入名称,它都会显示为大写第一个字母,小写则显示其余部分.例如,如果他们输入"joHN",程序仍然会输出他们的名字"John".
我有他们的名字输入以下代码:
string name;
cout << "Please enter your first name: ";
cin >> name;
Run Code Online (Sandbox Code Playgroud)
我假设我将不得不使用toupper和tolower命令,但我真的不确定如何写一些东西来调整字符串中的每个字符.任何帮助,将不胜感激.
谢谢.
我在C中创建自己的toUpper函数,但每次尝试运行时都会出现分段错误.任何人都可以向我提供有关如何解决此问题的任何提示吗?
int toUpper(char *str)
{
int i;
for(i=0;i< i <= strlen(str); i++) {
if(str[i] >= 97 && str[i] <= 122) {
str[i] = str[i] -32;
}
}
return(str[i]);
}
int main()
{
char string[20];
printf("Enter any string:\n");
scanf("%s", string);
char result= (char)(toUpper(string));
printf("The string in upper case is:%s\n", result);
}
Run Code Online (Sandbox Code Playgroud) 你们知道为什么下面的代码在运行时崩溃了吗?
char* word;
word = new char[20];
word = "HeLlo";
for (auto it = word; it != NULL; it++){
*it = (char) tolower(*it);
Run Code Online (Sandbox Code Playgroud)
我正在尝试小写char*(字符串).我正在使用visual studio.
谢谢
这里有什么问题吗?
我想检查char数组中的char是否较低,因此如果是,则应将其更改为大写。
#include <stdio.h>
int main(int argc, char *argv[]) {
char arr[100];
scanf("%s",&arr);
for(int i=0;i<sizeof(arr);i++){
if(int islower(arr[i])){
arr[i] = toupper(arr[i]);
}
}
printf("%s",arr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)