2012年12月30日

//整数转化为字符串数 && 字符串数转化为整数
#include
#include
#include
using namespace std;

void itoa()
{
    int num = 0;
    int i = 0;
    int j = 0;
    char a[100];    
    char b[100];
    if(num == 0)
            a[i++] = 0+'0';
    while(num)
    {
          a[i++] = num+'0';
          num /= 10;
    }
    i = i-1;
    while(i >= 0)
            b[j++] = a[i--];
    b[j] = '\0';
    cout << b << endl; 
}
void atoi()
{
      char s[] = " -34";
      int i = 0;
      int sign = 1;
      int sum = 0;
      if(s[i] == ' ' || s[i] == '\t')
              i++; 
      if(s[i] == '+')
      {
              sign = 1;
              i++;
      }
      else if(s[i] == '-')
      {
          sign = -1;
          i++;
      }
      while(s[i] != '\0') 
            sum = sum*10+(s[i++]-'0');
      cout << sum*sign << endl;
}
int main(int argc, char *argv[])
{
    itoa();
    atoi();
    system("PAUSE");
    return EXIT_SUCCESS;
}

//strcpy函数
#include <cstdlib>
#include <iostream>

using namespace std;
char *strcpy(char *b, char *a)
{
      assert((a != NULL) && (b != NULL));
      char *address = b;
      while((*b++ = *a++) != '\0');
      return address;
}
int main(int argc, char *argv[])
{
    char a[] = "I love Boat";
    char b[100];
    char *address = strcpy(b, a);
    int length = strlen(address);
    cout << address << endl;
    cout << length << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

//将一个字符串向右移动两位:考察memcpy,strcpy,memset等库函数的使用。
#include <cstdlib>
#include <iostream>

using namespace std;

char *loopmove(char *str, int steps)
{
      int n = strlen(str)-steps;
      char temp[100];
      memcpy(temp, str+n, steps);
      memcpy(temp+steps, str, n);
      temp[strlen(str)] = '\0';
      cout << temp << endl;    
}
int main(int argc, char *argv[])
{
    char a[] = "abcdefghi";
    loopmove(a, 2);
    system("PAUSE");
    return EXIT_SUCCESS;
}

//输入一行字符串,找出其中出现的相同且长度最长的字符串
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
    string a = "yyabcdabjcabceg";
    string temp;
    int max = 0;
    string fin; 
    for(int i = a.length(); i >= 0; i--)
    {
            for(int j = 0; j < a.length(); j++)
            {
                    if(j <= i)
                    {
                          int t = 0;
                          int num = 0;
                          temp = a.substr(j, i);
                          //cout << temp << endl;
                          t = a.find(temp);
                          num = a.rfind(temp);
                          if((t != num) && (temp.length() > max))
                          {
                                max = num-t;
                                fin = temp;
                         
                   
           
   
    cout << fin << " " << fin.length() << endl;                    
    system("PAUSE");
    return EXIT_SUCCESS;
}

//实现C++中的strstr(),即把主串中子串及以后的字符全部返回。不使用已有的函数来完成。
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

char *strstr(char *a, char *b)
{
    for(int i = 0; a[i] != '\0'; i++)
    {
            int j = 0;
            if(a[i] == b[j])
            {
                    int temp = i;
                    while(a[i++] == b[j++])
                    {
                                  if(b[j] == '\0')
                                          return a+i-j;
                    }
                    i = temp;
            }
    }
    return NULL;
}
int main(int argc, char *argv[])
{
    char a[] = "1123412345678";                  
    char b[] = "234";
    cout << strstr(a, b) << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}


//将一句话里的单词进行倒置,标点符号不倒换。
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;


int main(int argc, char *argv[])
{
    char a[] = "i come from tianjin.";
    int i = 0;
    int begin, end;
    int j = strlen(a)-1;
    char temp;
    while(j > i)
    {
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
            j--;
            i++;
    }
    i = 0;
    while(a[i] != NULL)
    {
                if(a[i] != ' ')
                {
                        begin = i;
                        while(a[i] != ' ' && a[i] != NULL)
                                  i++;
                        i = i - 1;
                        end = i;
                }
                while(begin < end)
                {
                            temp = a[begin];
                            a[begin] = a[end];
                            a[end] = temp;
                            end--;
                            begin++;
                }
                i++;
    }
    cout << a << endl;                    
    system("PAUSE");
    return EXIT_SUCCESS;
}

//转换字符串格式为原来字符串里的字符+该字符连续出现的个数。可以使用sprintf(),打印到字符串中。
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;


int main(int argc, char *argv[])
{
    char a[] = "1233422222";
    char b[100];
    b[0] = '\0';
    int len = strlen(a);
    int count = 1;
    for(int k = 0; k <= len-1; k++)
    {
            if(a[k+1] == a[k])
            {
                      count++;
            }
            else
            {
                sprintf(b+strlen(b), "%c%d", a[k], count);
                count = 1;
            }
    }
    cout << b << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值