C/PTA —— 13.指针2(课内实践)
一.函数题 6-1使用函数实现字符串部分复制 6-2 拆分实数的整数部分和小数部分 6-3 存在感
二.编程题
一.函数题
6-1使用函数实现字符串部分复制
void strmcpy ( char * t, int m, char * s)
{
int len = 0 ;
char * ret = t;
while ( * ret != '\0' )
{
ret++ ;
len++ ;
}
if ( m > len)
* s = '\0' ;
else
{
t = t + m - 1 ;
while ( * t != '\0' )
{
* s = * t;
s++ ;
t++ ;
}
* s = * t;
}
}
6-2 拆分实数的整数部分和小数部分
void splitfloat ( float x, int * intpart, float * fracpart)
{
int num1 = ( int ) x;
* intpart = num1;
float num2 = x- ( float ) num1;
* fracpart = num2;
}
6-3 存在感
int frequency ( char * paragraph, char * from, char * to)
{
int x = 0 ;
for ( int i = 0 ; paragraph[ i] != '\0' ; i++ )
{
if ( paragraph[ i] == * from)
{
int flag = 1 ;
for ( int j = 0 ; j < ( to - from + 1 ) ; j++ )
if ( paragraph[ i + j] != * ( from + j) )
{
flag = 0 ;
break ;
}
if ( flag)
x++ ;
}
}
return x;
}
二.编程题
7-1 单词反转
# include <stdio.h>
# include <string.h>
int main ( )
{
char ch[ 999 ] ;
gets ( ch) ;
int len = strlen ( ch) , k = 0 , i, j, flag1 = 0 , flag2 = 0 ;
ch[ len] = ' ' ;
while ( k <= len)
{
if ( ch[ k] != ' ' && flag1 == 0 )
{
i = k;
flag1 = 1 ;
}
if ( ch[ k] == ' ' && flag2 == 0 && flag1 == 1 )
{
flag2 = 1 ;
j = k - 1 ;
}
if ( flag1 == 1 && flag2 == 1 )
{
flag1 = 0 ;
flag2 = 0 ;
for ( ; j >= i; j-- )
printf ( "%c" , ch[ j] ) ;
printf ( " " ) ;
}
k++ ;
}
return 0 ;
}