一些查找和判断的C编程题
来源:互联网 发布:正规淘宝小号交易平台 编辑:程序博客网 时间:2024/05/17 00:00
1 查找字符串str中有多少个指定的子串str1
方法一:遍历(如果str1只有一个字符)
c1 = 0;
for (int i = 0; i < str.Length; i++)
{
if (str[i] == 'A')
{
c1++;
}
}
方法二:把要str1用空格取代,那么str少掉的数目就是str中含str1的数目
str.Length - str.Replace(str1, String.Empty).Length;
2 统计给定文本的单词个数
算法:
(1),与字母的ascii码做比较得出是否字母。
(2),设置是否进入单词标志,如果是字母,单词标志设为false,如果非字母(单词开始),设为true,进入count++
public static int WordCount(string strInput)
{
int count=0;
//单词开始标志
bool WordStart=true;
for(int i=0;i<strInput.Length;i++)
{
char a1=strInput[i];
if(((((int)a1)>=65)&&(((int)a1)<=90))||((((int)a1)>=97)&&(((int)a1)<=122)))
{
//判断是否是字母
if(WordStart)
{
//已经进入一个单词,后面的字母不计数
WordStart=false;
count++;
}
}
else
{
//非字母,单词开始标志复位
WordStart=true;
}
}
return count;
}
3 在一篇文档里找出某字符串的所有行号和个数
* 参数:
* fd, 打开的文件句柄
* str,指向要查找的字符串。
* 返回值: 出现的个数
int findstr(FILE* fd,char *str)
{
int c;
int line=0;
int col=0;
int count=0;
char *p=str;
while((c=fget(fd)!=EOF)
{
if(c==*p)
{
p++;
while(*p!='/0'&&(c=fget(fd)!=*p) p++,col++;
if(*p=='/0')
{
count++;
printf("num %d line %d, col %d /n",count,line+1,col+1);
}
}
if(c==13)
col=0;
if(c=='/n')
{
line++;
col=0;
continue;
}
col++;
}
printf("Total :%d/n",count);
return count;
}
4 写一函数,检测一个字符串是否有一个回环串?(比如abcdedcba)
#include <iostream.h>
#include <string.h>
bool Test(char* src)
{
for(int i = 0 ; i<int(strlen(src)+1)/2 ; i++)
{
if(src[i] != src[strlen(src)-1-i])
return false;
}
return true;
}
int main()
{
cout<<Test("abcdedcbaa")<<endl;
return 0;
}
5 判断一个链表是否为循环链表
#include <malloc.h>
/*目的:检测指定的链表中是否存在循环*/
/*算法概要:同时指定p1,p2指向头节点,p1步长为1向后移,p2步长为2向后移*/
/*如果p1或p2指向NULL,说明不存在循环*/
/*如果存在循环,则p2经过循环必然会追上p1*/
/*算法的效率不是很高,但是却很简洁*/
/*author:dirtysalt,date:05.7.17,time:6:10*/
///结点结构
{
struct list *next;
}list;
list *head=NULL,*tail=NULL;
void new_list()
{
tail=(list*)malloc(sizeof(list));
tail->next=NULL;
head=tail;
}
void add_item()
{
list *item;
item=(list*)malloc(sizeof(list));
item->next=head;
head=item;
}
void free_list()
{
list *tmp;
while(head!=tail)
{
tmp=head;
head=head->next;
free(tmp);
}
free(head);
}
void make_loop()
{
tail->next=head;
}
int check_list_loop()
/*return value 1:loop*/
/*0:!loop*/
{
list *p1=head,*p2=head;
while(((p1=p1->next)!=NULL)&&((p2=p2->next)!=NULL)&&((p2=p2->next)!=NULL))
{
if(p1==p2)
return 1;
}
return 0;
}
int main(int argc, char *argv[])
{
int i;
new_list();
for(i=0;i<10;i++)
add_item();
if(check_list_loop()==1)
printf("List Loop/n");
else
printf("List Non-Loop/n");
make_loop();
if(check_list_loop()==1)
printf("List Loop/n");
else
printf("List Non-Loop/n");
free_list();
return 0;
}
- 一些查找和判断的C编程题
- c和c++的一些训练题(11)(二分查找法和顺序查找法)
- c语言的一些语句说明和查找问题
- 【C语言】再次找的一些C语言题目,关于折半查找和某些种种
- 使用PRO*C编程的一些说明和例子
- 范围查找 和最近的一些感想
- 用c语言实现闰年的查找或判断
- Linux C编程--操作文件结束和错误的判断方法(函数)
- C++Builder 的一些高级编程技巧
- C专家编程上的一些习题
- C++Builder 的一些编程技巧
- C++Builder 的一些高级编程技巧
- C 编程一些关键字的认识
- C/C++编程的一些问题
- C 专家编程的一些小知识点
- 一些C基础的小编程题目
- C语言实现的线性查找和二分查找
- 关于使用PRO*C编程的一些简单说明和例子
- asdasd
- Mp3tag 2.37新版发布
- SQL server: 用户'sa'登录失败。原因:未与信任SQL Server连接相关联。。
- 前世的瞎眼换来今生的与你相遇
- Hibernate 2 升级为Hibernate 3 的注意事项
- 一些查找和判断的C编程题
- JavaScript之内存释放
- javascrip
- c++类计算减法
- 什么是AJAX
- FireBug ajax
- 深圳深信服科技07年校园招聘笔试题目
- VC&MATLAB混合编程
- 用VS.NET2003制作WEB应用程序的安装包[转]