第一篇博客文章——C语言
来源:互联网 发布:linux ftp目录设置 编辑:程序博客网 时间:2024/05/09 14:47
到了本命年的年纪,也到了研一的时段。之前从未写过有关技术方面的东西,因为懒,更因为自己在技术这条道路上实在是没有学到什么东西,完全拿不出手。研究生一年级了,却还像个大学一年纪的小孩,看着简单的C语言发呆,硬是想不出怎么解。从大二开始学习C语言,也学习了数据结构,虽然考试也考得了不错的成绩,但真正让自己去学习写程序,去分析问题,头都开始变大了。今天是第一天,以后都会坚持写有关C语言,有关Java方面的东西。菜鸟一个,肯定做不出什么高大上的成果,但坚持下去总会有些效果。
NO.1
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
比如字符串“abacacde”过滤结果为“abcde”。
要求实现函数:
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
程序如下:#include<stdio.h>
#include<string.h>
#include<malloc.h>
void stringZip(const char *pInputStr, long IinputLen, char *pOutputStr) //先定义
{
int count = 1;
for (int i = 0; i < IinputLen; i++)
{
if (pInputStr[i] == pInputStr[i+1])
{
count++;
}
else
{
if (count>1)
{
*pOutputStr++ = count + '0'; //将整数转换为字符串,但是如果count>9,将会出现字母,所以不严谨。
*pOutputStr++ = pInputStr[i];
}
else
{
*pOutputStr = pInputStr[i];
pOutputStr++;
}
count = 1;
}
}
*pOutputStr = '\0';
}
void main()
{
char *pInputStr = "xxxxxzxxxxy";
printf("%s\n", pInputStr);
int IinputLen = strlen(pInputStr);
char *pOutputStr = (char*)malloc(IinputLen*sizeof(char));
stringZip(pInputStr, IinputLen, pOutputStr);
printf("%s\n", pOutputStr);
free(pOutputStr);
pOutputStr = NULL;
}
注:以下内容为转载
功 能:把一整数转换为字符串
用 法:char *itoa(int value, char *string, int radix);
详细解释:itoa是英文integer to array(将int整型数转化为一个字符串,并将值保存在数组string中)的缩写.
参数:
value: 待转化的整数。
radix: 是基数的意思,即先将value转化为radix进制的数,范围介于2-36,比如10表示10进制,16表示16进制。
* string: 保存转换后得到的字符串。
返回值:
char * : 指向生成的字符串, 同*string。
备注:该函数的头文件是"stdlib.h"
程序例:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int number = 123456;
char string[25];
itoa(number, string, 10);
printf("integer = %d string = %s\n", number, string);
return 0;
}
注释:编译系统:VC++6.0,TC不支持。
我们可以这样构造itoa()
char* itoa(int i)
{
char *a=malloc(42); /* Enough for a 128 bit integer */
if (a) sprintf(a,"%d",i);
return a;
}
实现itoa函数的源代码
char *my_itoa(int num,char *str,int radix){
const char table[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *ptr = str;
bool negative = false;
if(num == 0){ //num=0
*ptr++='0';
*ptr='\0'; // don`t forget the end of the string is '\0'!!!!!!!!!
return str;
}
if(num<0){ //if num is negative ,the add '-'and change num to positive
*ptr++='-';
num*=-1;
negative = true;
}
while(num){
*ptr++ = table[num%radix];
num/=radix;
}
*ptr = '\0'; //if num is negative ,the add '-'and change num to positive
// in the below, we have to converse the string
char *start =(negative?str+1:str); //now start points the head of the string
ptr--; //now prt points the end of the string
while(start<ptr){
char temp = *start;
*start = *ptr;
*ptr = temp;
start++;
ptr--;
}
return str;
}
- 第一篇博客文章——C语言
- C语言学习——第一篇博客 (一)
- C语言学习——第一篇博客 (二)
- C语言学习——第一篇博客 (三)
- C语言学习——第一篇博客(四)
- C语言学习——第一篇博客(五)
- 第一篇博客—c语言单链表的基本操作
- 第一篇博客文章
- 第一篇博客文章
- 第一篇博客文章
- 第一篇博客文章
- 第一篇博客文章
- 第一篇博客文章
- 第一篇博客文章
- 第一篇博客文章
- 第一篇博客文章
- 第一篇博客文章
- 博客文章第一篇
- 使用MediaPlayer和SurfaceView播放视频
- cloudstack源码部署时提示程序包com.sun.image.codec.jpeg不存在
- iOS AutoLayout(3)
- C编译器剖析_5.4.2 中间代码生成及优化_基本块的合并
- 详解原生JavaScript实现div拖拽功能
- 第一篇博客文章——C语言
- 分享_微博授权返回出现相同的分享界面
- [Play Framework]Manipulating Results——操作结果
- ubuntu安装samba和ssh, ftp
- 基于Protobuf的通讯库--Poppy简介
- Lucene系列-索引文件
- 很久没写博客了!今天来写个八方向摇杆基于quick cocos2d
- res:资源文件的配置文件
- 使用VS2010连接SQL Server 2008数据库混合编程以实现对数据库的操作