Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1413917
  • 博文数量: 241
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 22:27
个人简介

--

文章分类

全部博文(241)

文章存档

2021年(3)

2019年(6)

2018年(1)

2017年(9)

2016年(21)

2015年(50)

2014年(125)

2013年(26)

我的朋友

分类: C/C++

2014-03-21 16:30:50

1、题目描述
输入字符串,将字符串分割成没8个字节的长度,多余的部分归到下一个字符串,不够8个的用‘0’补全。
接口实现:
int  AddString(char *strValue)//添加一条字符串
int  GetLength() //获取分割后字符串长度
int  ArrCmp(char strInput[][9],int iLen) //检查最终的分割后字符串和给定的strInput是否一致
2、代码:字符串操作,基本问题

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "oj.h"
  4. #include <string.h>
  5. #define N 50
  6. #define M 8
  7. int count = 0;
  8. char str[N][100]={0};

  9. /*****************************************************************
  10. 功能:存储输入的字符创

  11. 输入:字符串

  12. 输出:
  13.      
  14. 返回:0表示成功,其它返回-1
  15. ****************************************************************/
  16. int AddString(char *strValue)
  17. {
  18.     int len;
  19.     int i;
  20.     if (NULL == strValue)
  21.     {
  22.         return -1;
  23.     }

  24.     len = strlen(strValue);
  25.     if (len <= 0)
  26.     {
  27.         return -1;
  28.     }
  29.     while (len > M)
  30.     {
  31.         strncpy(str[count],strValue,M);
  32.         str[count][M]='\0';
  33.         count++;
  34.         len -= M;
  35.         strValue += M;
  36.     }
  37.     if (len != 0)
  38.     {
  39.         strncpy(str[count],strValue,M);
  40.         for (i=len; i<M; i++)
  41.         {
  42.             str[count][i]='0';    
  43.         }
  44.         str[count][M]='\0';    
  45.         count++;
  46.     }
  47.     return 0;
  48. }

  49. /****************************************************************
  50. 功能:获取补位后的二维数组的长度

  51. 输入:

  52. 输出:
  53.      
  54. 返回:二维数组长度
  55. ******************************************************************/
  56. int GetLength()
  57. {
  58.     return count;
  59. }

  60. /*****************************************************************************
  61. 功能:将补位后的二维数组,与输入的二维数组做比较

  62. 输入:strInput:输入二维数组,iLen:输入的二维数组的长度

  63. 输出:
  64.      
  65. 返回:若相等,返回0;不相等,返回-1.其它:-1;
  66. ******************************************************************************/
  67. int ArrCmp(char strInput[][9],int iLen)
  68. {
  69.     if (strInput == NULL || iLen <= 0)
  70.     {
  71.         return -1;
  72.     }
  73.     int i;
  74.     for (i=0; i<iLen; i++)
  75.     {
  76.         if (strncmp(strInput[i],str[i],M) == 0)
  77.         {
  78.             continue;
  79.         }
  80.         else
  81.         {
  82.             return -1;
  83.         }
  84.     }
  85.     
  86.     count = 0;
  87.     memset(str,'\0',sizeof(str));
  88.     return 0;
  89. }
阅读(853) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~