一些查找和判断的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 <stdio.h>
#include <malloc.h>
/*目的:检测指定的链表中是否存在循环*/
/*算法概要:同时指定p1,p2指向头节点,p1步长为1向后移,p2步长为2向后移*/
/*如果p1或p2指向NULL,说明不存在循环*/
/*如果存在循环,则p2经过循环必然会追上p1*/
/*算法的效率不是很高,但是却很简洁*/
/*author:dirtysalt,date:05.7.17,time:6:10*/
///结点结构
typedef struct list
{
struct list *next;
}list;
///定义head,tail结点
list *head=NULL,*tail=NULL;
///建立一个链表头尾结点对象,相当于c++里new
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;
}
///判断是否循环链表,返回1为loop
///*p1和*p2同时指向头指针,p1=p1->next,p2=p2->next,p2=p2->next,这样递增,如果可以指向同样////的///结点就是循环结点,否则不是
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;
}
 
原创粉丝点击