字符串字母大小写互换、查找指定字符、大写字母倒换

字符串字母大小写互换

#include <stdio.h>
#include <string.h>
int main()
{
	int i=0;
	char a[100];
	while ((a[i] = getchar()) != '#') i++;
		for (int j = 0; j < i; j++)
		{
			if (a[j] >= 'A'&&a[j] <= 'Z')
			{
				a[j] += 32;
			}
			else if (a[j] >= 'a'&&a[j] <= 'z')
			{
				a[j] -= 32;
			}
		}
		for (int j = 0; j < i; j++) printf("%c", a[j]);
	return 0;
}

查找指定字符

#include <stdio.h>
#include <string.h>
int main()
{
	int i=0,len,flag=0;
	char a[100],x;
	scanf("%c\n", &x);
	gets(a);
	len = strlen(a);
	for (i = len; i >=0; i--)
	{
		if (a[i] == x)
		{
			printf("index = %d\n", i);
			flag = 1;
			break;
		}
	}
	if (flag == 0) printf("Not Found\n");
	return 0;
}

大写字母倒换

#include<stdio.h>
#include<string.h>
int main()
{
	char s[100];
	char C = 'A',C2='Z';
	gets_s(s);
	int count=0,len,j;
	len = strlen(s);
	for (int i = 0; i < len; i++)
	{
		if (s[i] <= 'M'&&s[i] >= 'A')
		{
			count = 0;
			C = 'A';
			while (C != s[i])
			{
				C++;
				count++;
			}
			s[i] = s[i] + 25 - count*2;
			continue;
		}
		if (s[i] <= 'Z'&&s[i] >= 'M')
		{
			count = 0;
			C2 = 'Z';
			while (C2 != s[i])
			{
				C2--;
				count++;
			}
			s[i] = s[i] - 25 + count * 2;
		}
	}
	for (j = 0; j < len; j++) printf("%c", s[j]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44017102/article/details/87606894