• 字符串


    1.字符串逆置(字符串回文-字符串逆置后与原字符串比较)

    用指针实现:

    int main(int argc, char* argv[])
    {
      char *str = "hello world";
      int len = strlen(str);
      //分配内存
      //将src拷贝到新分配的内存中
      char * copy = (char*)malloc(len+1);
      int i = len -1;
      while(i>=0)
      {
        *(copy + len-1-i) = *(str + i);
        i--;
      }
      *(copy + len) = '';
      cout<<copy<<endl;

      return 0;
    }

    int main()

    {

      char str[] = "hello world";

      int len = strlen(str);

      char *begin = str;

      char *end = str+len-1;

      while(p1<p2)

      {

        char c = *p1;

        *p1 = *p2;

        *p2 = c;

        ++p1;

        --p2;

      }  

      printf("%s ",str);

      return 0; 

    }

    用数组实现:

    int main()

    {

      char s[] = "hello world";
      int n=strlen(s),i,k;char t;
      k=n;
      for(i=0;i<k--;i++)
      {
        t=s[i];
        s[i]=s[n-1-i];
        s[n-1-i]=t;
      }
      printf("%s ",s);
      return 0;

    }

    int main(int argc, char* argv[])
    {
      char* str="hello world";
      int len=strlen(str);
      char *s=new char[len+1];
      int j=len-1;
      for(int i=0;i<len;i++)
      {
        s[i]=str[j];j--;
      }
      *(s+len)='';
      printf("%s",s);
      return 0;
    }

    void reverse()
    {
      // 不能是char *str = "dddagge";,这时的"dddagge"是字符串常量
      char str[] = "dddagge";
      int lenth = strlen(str);
      int i = 0;
      char temp;
      while(i <= (lenth-1)/2)
      {
        temp = *(str + i);
        *(str + i) = *(str + lenth - 1 - i);
        *(str + lenth -1 - i) = temp;
        i++;
      }
      printf("%s",str);
    }

    //递归实现
    char *strrv3(char *str,int len)
    {
     if (len<=1)
      return str;
     char t=*str;
     *str=*(str+len-1);
     *(str+len-1)=t;
     return (strrv3(str+1,len-2)-1);
    }

    2.写一个函数将"tom is cat" 倒序打印出来,即 "cat is tom"

     1 <span style="font-size:14px;color:#000000;">#include "stdafx.h"
     2 #include <iostream>
     3 
     4 using namespace std;
     5 
     6 //反字符串
     7 #define SPACE ' '  //或者使用const char SPACE = ' ';
     8 int main()
     9 {
    10 
    11     char* str = "Tom is cat fgfgfgfg juytf"; // 字符串
    12     char* first= str+strlen(str)-1;
    13     char* second = first + 1; // 开始时 俩都指向字符串结尾处
    14     while(str != first--)       //指针从结尾处开始循环递减到 字符串为空的
    15     {
    16         if(SPACE == *first)     //第一个单词
    17         {            
    18             for (int i = 1; first + i != second; ++i)
    19             {
    20                 cout << *(first + i); 
    21             }
    22             cout << " ";
    23             second = first;
    24         }
    25         if (str == first)
    26         {
    27             for (int i = 0; first + i != second; ++i)
    28             {
    29                 cout << *(first + i);  
    30             }
    31             cout << endl;
    32         }
    33     }
    34 }

    3.字符串的排序与交换 冒泡。。
    void sort1(char *src,int num)
    {
     int i,j;
     int tmp=0;
     for (i=0;i<num;i++)
     {
      for (j=i+1;j<num;j++)
      {
       tmp=src[i];
       src[i]=src[j];
       src[j]=tmp;
      }
     }
    }

    4.整数与字符串互转

    int main(int argc, char* argv[])
    {
      int num = 12345,j=0,i=0,sum=0;
      char temp[7] = {'1','2','3','4','5',''};
      while(temp[i])
      {
        sum = sum*10 + (temp[i]-'0');
        i++;
      }
      printf("sum = %d ",sum);
      return 0;
    }

    整数转化为字符串,可加‘0’再逆序。

    int main(int argc, char* argv[])
    {
      //数字转行成字符串
      int num = 12345,j=0,i=0;
      char temp[7],str[7];
      while(num)
      {
        temp[i] = num%10 + '0';
        i++;
        num = num/10;
      }
      temp[i] = '';
      printf("temp=%s",temp);

      //然后逆序
      i = i-1;
      while(i>=0)
      {
        str[j] = temp[i];
        j++;
        i--;
      }
      str[j]=0;
      printf("str=%s",str);

      return 0;
    }

    5.删除子串(查找子串第一个位置也可以参考)

    int delete_sub_str(const char* str,const char* sub_str,char* result_str)
    {
      int count= 0;
      int k = 0;
      char *result = result_str;
      for(int i =0;str[i]!='';i++)
      {
        int tem = i;
        int j=0;
        while((sub_str[j]!='')&&(str[i]==sub_str[j]))
        {
          i++;
          j++;
        }
        if(sub_str[j]!='')
        {
          i=tem;
          result[k] = str[i];
          k++;
        }
        else
        {
          count+=1;
          i--;
        }
      }
      return count;
    }
    int main(int argc, char* argv[])
    {
      char* str = "12fuck34fuck435fuck5431";
      char* sub = "fuck";
      char res[50] = "";
      int count = delete_sub_str(str,sub,res);
      printf("%d,after delete str=%s ",count,res);
      return 0;
    }

    6.统计字符串中数字个数,如122333444455555,输出5,5

    int main(int argc, char* argv[])
    {
      char str[32] = "1234346548798354368006524264790";

      int len = strlen(str);
      int count_total = 0;
      char t;

      for(int i=0; i<len; i++)
      {
        int tmp = i;
        int count = 0;
        for(int j=tmp; j<len-tmp; j++)
        {
          if(str[tmp] == str[j])
          {
            count++;
          }
        }
        if(count_total<count)
        {
          count_total=count;
          t = str[tmp];
        }
      }

      printf("%c ",t);
      printf("%d ",count_total);
      return 0;
    }

  • 相关阅读:
    IE浏览器cookie存放在本地什么目录
    C# clickonce 部署自定义文件
    [C#.Net]C#连接Oracle数据库的方法
    Eclipse 插件一览
    登录名 '***' 拥有一个或多个数据库。在删除该登录名之前,请更改相应数据库的所有者。 (Microsoft SQL Server,错误: 15174)
    SqlServer_表结构查询_查询当前用户所有表名
    sqlserver怎么删除有外键关联的数据?
    springboot logback控制台中文输出乱码
    控制台运行JAVA的jar文件时乱码 / Gradle编译设置编码格式
    Python 算法之一
  • 原文地址:https://www.cnblogs.com/SnailProgramer/p/4835743.html
Copyright © 2020-2023  润新知