2012年12月30日

来源:互联网 发布:深度linux硬盘怎么分区 编辑:程序博客网 时间:2024/06/06 08:26
//整数转化为字符串数 && 字符串数转化为整数
#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()
{
     chars[] = " -34";
     int i= 0;
     intsign = 1;
     intsum = 0;
    if(s[i] == ' ' || s[i] == '\t')
           i++; 
    if(s[i] == '+')
    {
           sign = 1;
           i++;
    }
     elseif(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");
    returnEXIT_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');
     returnaddress;
}
int main(int argc, char *argv[])
{
    char a[] = "I loveBoat";
    char b[100];
    char *address =strcpy(b, a);
    int length =strlen(address);
    cout<< address<< endl;
    cout<< length<< endl;
    system("PAUSE");
    returnEXIT_SUCCESS;
}

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

using namespace std;

char *loopmove(char *str, int steps)
{
     int n= strlen(str)-steps;
     chartemp[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");
    returnEXIT_SUCCESS;
}

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

int main(int argc, char *argv[])
{
    string a ="yyabcdabjcabceg";
    string temp;
    int max = 0;
    stringfin; 
    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");
    returnEXIT_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')
                                   returna+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");
    returnEXIT_SUCCESS;
}


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


int main(int argc, char *argv[])
{
    char a[] = "i come fromtianjin.";
    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");
    returnEXIT_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");
    returnEXIT_SUCCESS;
}


原创粉丝点击