# include <math.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <time.h>
# include "chrpak.h"
/******************************************************************************/
int a_to_i4 ( char ch )
/******************************************************************************/
/*
Purpose:
A_TO_I4 returns the index of an alphabetic character.
Example:
CH A_TO_I4
'A' 1
'B' 2
...
'Z' 26
'a' 27
'b' 28
...
'z' 52
'$' 0
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
01 July 2008
Author:
John Burkardt
Parameters:
Input, char CH, a character.
Output, int A_TO_I4, is the alphabetic index of the character,
between 1 and 26 if the character is a capital letter,
between 27 and 52 if it is lower case, and 0 otherwise.
*/
{
if ( 'A' <= ch && ch <= 'Z' )
{
return ( ( int ) ( ch - 'A' + 1 ) );
}
else if ( 'a' <= ch && ch <= 'z' )
{
return ( ( int ) ( ch - 'a' + 26 + 1 ) );
}
else
{
return 0;
}
}
/******************************************************************************/
int base_to_i4 ( char *s, int base )
/******************************************************************************/
/*
Purpose:
BASE_TO_I4 returns the value of an integer represented in some base.
Discussion:
BASE = 1 is allowed, in which case we allow the digits '1' and '0',
and we simply count the '1' digits for the result.
Negative bases between -16 and -2 are allowed.
The base -1 is allowed, and essentially does a parity check on
a string of 1's.
Example:
Input Output
------------- ------
S BASE I
------ ----- ------
'101' 2 5
'-1000' 3 -27
'100' 4 16
'111111' 2 63
'111111' -2 21
'111111' 1 6
'111111' -1 0
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
27 July 2000
Author:
John Burkardt
Parameters:
Input, char *S, the string. The elements of S are
blanks, a plus or minus sign, and digits. Normally, the digits
are representations of integers between 0 and |BASE-1|. In the
special case of base 1 or base -1, we allow both 0 and 1 as digits.
Input, int BASE, the base in which the representation is given.
Normally, 2 <= BASE <= 16. However, there are two exceptions.
Output, int BASE_TO_I4, the integer.
*/
{
char c;
int i;
int ichr;
int idig;
int isgn;
int istate;
int nchar;
nchar = s_len_trim ( s );
if ( base == 0 )
{
printf ( "\n" );
printf ( "BASE_TO_I4 - Serious error!\n" );
printf ( " The input base is zero.\n" );
i = -1;
return i;
}
if ( 16 < abs ( base ) )
{
printf ( "\n" );
printf ( "BASE_TO_I4 - Serious error!\n" );
printf ( " The input base is greater than 16!\n" );
i = -1;
return i;
}
i = 0;
istate = 0;
isgn = 1 ;
ichr = 1;
while ( ichr <= nchar )
{
c = s[ichr-1];
//
// Blank.
//
if ( c == ' ' )
{
if ( istate == 2 )
{
break;
}
}
//
// Sign, + or -.
//
else if ( c == '-' )
{
if ( istate != 0 )
{
break;
}
istate = 1;
isgn = -1;
}
else if ( c == '+' )
{
if ( istate != 0 )
{
break;
}
istate = 1;
}
else
/*
Digit?
*/
{
idig = hex_digit_to_i4 ( c );
if ( abs ( base ) == 1 && ( idig == 0 || idig == 1 ) )
{
i = base * i + idig;
istate = 2;
}
else if ( 0 <= idig && idig < abs ( base ) )
{
i = base * i + idig;
istate = 2;
}
else
{
printf ( "\n" );
printf ( "BASE_TO_I4 - Serious error!\n" );
printf ( " Illegal digit = \"%c\"\n", c );
printf ( " Conversion halted prematurely!\n" );
i = -1;
return i;
}
}
ichr = ichr + 1;
}
/*
Once we're done reading information, we expect to be in state 2.
*/
if ( istate != 2 )
{
printf ( "\n" );
printf ( "BASE_TO_I4 - Serious error!\n" );
printf ( " Unable to decipher input!\n" );
i = -1;
return i;
}
/*
Account for the sign.
*/
i = isgn * i;
return i;
}
/******************************************************************************/
void byte_to_int ( unsigned char *bvec, unsigned int *ival )
/******************************************************************************/
/*
Purpose:
BYTE_TO_INT converts 4 bytes into an unsigned integer.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
22 November 2010
Author:
John Burkardt
Parameters:
Input, unsigned char *BVEC, is a pointer to a character string.
The contents of BVEC through BVEC+3 are the bytes of IVAL,
from high order to low.
Output, unsigned int IVAL, the integer represented by the bytes.
*/
{
int i;
*ival = 0;
for ( i = 0; i < 4; i++ )
{
*ival = *ival << 8;
*ival = *ival + *bvec;
bvec = bvec + 1;
}
return;
}
/******************************************************************************/
char ch_cap ( char ch )
/******************************************************************************/
/*
Purpose:
CH_CAP capitalizes a single character.
Discussion:
This routine should be equivalent to the library "toupper" function.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
19 July 1998
Author:
John Burkardt
Parameters:
Input, char CH, the character to capitalize.
Output, char CH_CAP, the capitalized character.
*/
{
if ( 97 <= ch && ch <= 122 )
{
ch = ch - 32;
}
return ch;
}
/******************************************************************************/
void ch_count_cvec_add ( int n, unsigned char cvec[], int count[256] )
/******************************************************************************/
/*
Purpose:
CH_COUNT_CVEC_ADD adds a character vector to a character count.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
01 July 2008
Author:
John Burkardt
Parameters:
Input, int N, the number of entries in the vector.
Input, unsigned char CVEC[n], a vector of characters.
Input/output, int COUNT[256], the character counts.
*/
{
int i;
for ( i = 0; i < n; i++ )
{
count[cvec[i]] = count[cvec[i]] + 1;
}
return;
}
/******************************************************************************/
int ch_eqi ( char ch1, char ch2 )
/******************************************************************************/
/*
Purpose:
CH_EQI is TRUE (1) if two characters are equal, disregarding case.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
13 June 2003
Author:
John Burkardt
Parameters:
Input, char CH1, CH2, the characters to compare.
Output, int CH_EQI, is TRUE (1) if the two characters are equal,
disregarding case and FALSE (0) otherwise.
*/
{
int value;
if ( 97 <= ch1 && ch1 <= 122 )
{
ch1 = ch1 - 32;
}
if ( 97 <= ch2 && ch2 <= 122 )
{
ch2 = ch2 - 32;
}
if ( ch1 == ch2 )
{
value = 1;
}
else
{
value = 0;
}
return value;
}
/******************************************************************************/
int ch_index_first ( char *s, char c )
/******************************************************************************/
/*
Purpose:
CH_INDEX_FIRST finds the first occurrence of a character in a string.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
03 June 2008
Author:
John Burkardt
Parameters:
Input, char *S, a pointer to a string to be searched.
Input, char C, the character to be sear
没有合适的资源?快使用搜索试试~ 我知道了~
C 代码 操纵字符和字符串.rar
共5个文件
c:3个
sh:1个
h:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 40 浏览量
2022-11-12
20:02:03
上传
评论
收藏 19KB RAR 举报
温馨提示
c++源代码,c源代码,测试可以
资源推荐
资源详情
资源评论
收起资源包目录
C 代码 操纵字符和字符串.rar (5个子文件)
chrpak
._chrpak_prb.c 4KB
chrpak.h 3KB
chrpak.c 103KB
._chrpak.c 4KB
chrpak.sh 186B
共 5 条
- 1
资源评论
卷积神经网络
- 粉丝: 339
- 资源: 8460
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功