① c语言中的字符型的运算。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void k_add(string a, string b, int k)
{
//将长的字符串放在前面
if (a.size() < b.size())
{
string temp = a;
a = b;
b = temp;
}
//对源字符串进行翻转
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
bool carry = false; //进位
//字符串相加
size_t i = 0;
for (; i < b.size(); i++)
{
if (i < b.size())
{
a[i] += b[i] - '0';
if (carry) a[i] += 1;
carry = false;
if (a[i] >= k + '0')
{
a[i] -= k;
carry = true;
}
}
}
for (; i < a.size(); i++)
{
if (carry) a[i] += 1;
carry = false;
if (a[i] >= k + '0')
{
a[i] -= k;
carry = true;
}
}
if (carry)
{
a.push_back('1');
}
//输出
bool begin = false;
for (int j = a.size() - 1; j >= 0 ; j--)
{
if (a[j] > '0')
{
begin = true;
}
if (begin)
{
cout << a[j];
}
}
cout << endl;
}
简单模拟即可
10进制测试
② C语言字符 运算
我觉得你那句while根本就不可能对~你应该用if语句来判断,如果按照你用while语句写的话,那就会一直循环,直到a的值小于'A',你应该用if语句,然后如果是小写的话,让它减去32,如果是大写,就不用动了:
比如你这么写:
char
a;
scanf("%c",&c);/*由用户输入a*/
if(a>'a'&&a<'z')
a=a-32;
printf("%c",a);
上面这个是主要的代码,楼主再完善完善,不懂再来问
③ c语言字符串运算
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,len,ok;
char str[1024]={'\0'};
do
{
ok=1;
printf("请输入一个字符串:\n");
gets(str);
i=0;
while('\0'!=str[i])
{
if(!isalpha(str[i]))
{
ok=0;
break;
}
i++;
}
}while(!ok);
len=strlen(str);
printf("字符串\"%s\"的长度是%d。\n",str,len);
//printf("字符串\"%s\"包含了以下元音字母:\n",str);
printf("字符串未进行元音字母循环加密(加密规则a->e->i->o->u->a)前是:\n%s。\n",str);
i=0;
while('\0'!=str[i])
{
if('a'==str[i])
{
str[i]='e';
}
else if('e'==str[i])
{
str[i]='i';
}
else if('i'==str[i])
{
str[i]='o';
}
else if('o'==str[i])
{
str[i]='u';
}
else if('u'==str[i])
{
str[i]='a';
}
i++;
}
printf("字符串进行元音字母循环加密(加密规则a->e->i->o->u->a)后是:\n%s。\n",str);
system("PAUSE");
return EXIT_SUCCESS;
}
④ 如何在c语言中计算字符串长度
C语言的字符串是由字符数组形式保存的,并约定'