Training4:字符串训练

1. 题目: 请编写一个C函数,该函数将一个字符串逆序                                                              

#include 
#include 

void reverse(char str[])
{
	int len;
	len = strlen(str);

	while(len > 0)
	{
		len--;
		printf("%c", str[len]);
	}
	printf("\n");
	
}

int main()
{
	char str[20];
	printf("enter a string( within 20 bytes):\n");
	scanf("%s", str);

	reverse(str);

    return 0;
}

      

 

 

2. 题目: 请编写一个C函数,该函数可以实现将一个整数转为任意进制的字符串输出

 

#include 

void int_char(int num)
{
	int tmp;
	int i;
	int n = 0;
	int a[1024];

	while(num)
	{
		a[n++] = num % 10;
		num /= 10;
	}
	for(i = (n - 1); i >= 0; i--)
	{
		printf("%d", a[i]);
	}
	printf("\n");
}

int main()
{
	int num;
	printf("enter a number:\n");
	scanf("%d", &num);

	int_char(num);

    return 0;
}

#include 

void int_float(char str[], int a[], float b[], int *m, int *n)
{
	*m = 0;
	*n = 0;
	int tmp1;
	float tmp2;
	int i = 0;

	while(str[i] != '\0')
	{
		while((str[i] <= '9' && str[i] >= '0') || str[i] == '.')
		{
			tmp1 = 0;
			tmp2 = 0;
			while(str[i] <= '9' && str[i] >= '0')
			{
				tmp1 = tmp1 * 10 + str[i] - '0';
				i++;
			}
			if(str[i] == '.')
			{
				tmp2 = tmp1;
				float k = 0.1;
				i++;
				while(str[i] <= '9' && str[i] >= '0')
				{
					tmp2 = tmp2 + (str[i] - '0') * k;
					k *= 0.1;
					i++;
				}
				b[(*n)++] = tmp2;
			}
			else
			{
				a[(*m)++] = tmp1;
			}
			i++;
		
		}
		i++;
	}

}

void display(int a[], float b[], int *m, int *n)
{
	int i;
	printf("the integers is:\n");
	for(i = 0; i < *m; i++)
	{
		printf("%d", a[i]);
	}

	printf("\nthe float is:\n");
	for(i = 0; i < *n; i++)
	{
		printf("%.3f", b[i]);
	}
	printf("\n");
}


int main()
{
	int m, n;
	char str[1024];
	int a[1024];
	float b[1024];

	printf("enter a string:\n");
	scanf("%s", str);

	int_float(str, a, b, &m, &n);
	display(a, b, &m, &n);

    return 0;
}

 

 

3. 题目: 输入一个字符串,计算字符串中子串出现的次数

 

#include 
#include 
int func(char s[], char str[])
{
	int i = 0;
	int j = 0;
	int num = 0;
	int count = 0;

	int len;
	len = strlen(str);

	while(s[i] != '\0')
	{
		if(str[j] == s[i])
		{
			num++;
			j++;
			i++;
		if((num % len) == 0)
			{
				count++;
				j = 0;
			}
		}
		else if(num != 0)
		{
			num = 0;
			j = 0;
		}
		else
		{
			i++;
		}
	}
	return count;
}

int main()
{
	char s[1024];
	char str[1024];

	printf("enter a string :\n");
	scanf("%s", s);
	printf("enter a short string you want to find:\n");
	scanf("%s", str);

	printf("the string appears %d times\n", func(s, str));
    return 0;
}

 

 

4. 题目: 编写一个C函数,”I am from shanghai ”倒置为”shanghai from am I”,及将句子中的单词位置倒置,而不改变单词内部结构.

 

#include 
#include 

void reverse(char a[], int len)
{
	int i, j;
	char tmp = 0;
	int n = len - 1;

	for(i = 0; i < n; i++)
	{
		len--;
		for(j = 0; j < len; j++)
		{
			tmp = a[j];
			a[j] = a[j + 1];
			a[j + 1] = tmp;
		}
	}
}

void re_reverse(char *a)
{
	int n = 0;
	while(1)
	{
		n++;
		if((*a) == ' ' || (*a) == '\0')
		{
			reverse(a - n + 1, n - 1);
			n = 0;
		}
		if((*a) == '\0')
		{
			break;
		}
		a++;
	}
}

int main()
{
	char a[] = "I am from shanghai";
	int len;
	len = strlen(a);
	reverse(a, len);
	re_reverse(a);

	printf("the string after change is:%s\n", a);
    return 0;
}

 

 

5. 题目: 输入一个字符串,同时输入帧头和帧尾(可以是多个字符),将该字符串中合法的帧识别出来.

提示:帧头和帧尾分别是headtail  字符串”asdheadhauboisoktail”中headhauboisoktail是合法帧

 

#include 
#include 

int flag_h = 0;
int flag_t = 0;

char * head(char *s, char *h)
{
	int num_h = 0;
	int len_h = 0;
	char *tmp = h;

	len_h = strlen(h);
	if(s == NULL || h == NULL)
	{
		return NULL;	
	}
	if(strlen(s) < strlen(h))
	{
		return NULL;
	}
	while(*s != '\0' && flag_h == 0)
	{
		while(*s == *tmp)
		{
			num_h++;
			s++;
			tmp++;
			if(num_h == len_h)
			{
				flag_h = 1;
				tmp = s - num_h;
				break;
			}
		}
		if(num_h != 0 && num_h != len_h)
		{
			num_h = 0;
			tmp = h;
			s--;
		}
		s++;
	}
	if(flag_h == 1)
	{
		return tmp;
	}
	else 
	{
		printf("illegal string!\n");
	}
}

char * tail(char *s, char *t)
{
	int num_t = 0;
	int len_t;
	char *tmp = t;

	len_t = strlen(t);

	if(s ==	NULL || t == NULL)
	{
		return NULL;
	}
	if(strlen(s) < strlen(t))
	{
		return NULL;
	}
	if(flag_h == 1)
	{
		while(*s != '\0' && flag_t == 0)
		{
		
			while(*s == *tmp)
			{
				s++;
				tmp++;
				num_t++;
				if(num_t == len_t)
				{
					flag_t = 1;
					tmp = s - 1;
					break;
				}
			}
			if(num_t != 0 && num_t != len_t)
			{
				num_t = 0;
				tmp = t;
				s--;
			}
			s++;
		}
	}
	if(flag_t == 1)
	{
		return tmp;
	}
	else
	{
		printf("illegal string!\n");
		return NULL;
	}

}

void display(char *start, char *end)
{
	int n;
	int i;
	n = end - start + 1;
	if(n != 0)
	{
		for(i = 0; i < n; i++)
		{
			printf("%c", *start);
			start++;
		}
		printf("\n");
	}
	else
	{
		printf("no data!\n");
	}
}

int main()
{
	char *start = NULL;
	char *end = NULL;
	char s[100];
	char h[] = "head";
	char t[] = "tail";

	printf("enter a string(<100):\n");
	scanf("%s", s);

	start = head(s, h);
	end = tail(s, t);
	display(start, end);


    return 0;
}

你可能感兴趣的:(字符串逆序,整数转为任意进制,子串次数,倒置,合法的帧识别,Linux,c)