国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
C語言字符數(shù)組實(shí)用處理代碼


http://www.jb51.net/article/83010.htm 

作者:hzy3774   時(shí)間:2016-04-25 

這篇文章主要介紹了一波C語言字符數(shù)組實(shí)用技巧集錦,包括許多字符的轉(zhuǎn)換與提取等基本操作示例,需要的朋友可以參考下

字符數(shù)組倒序

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
 
 void daoxu(char str[]){
   int i;
   char temp;
   for(i = 0; i < strlen(str) / 2 ; i ++){
     temp = str[i];
     str[i] = str[strlen(str) - i-1];
     str[strlen(str) - i-1] = temp;
   }
 }

單詞計(jì)數(shù)   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int wordCount(char str[]){
  int i;
  int count = 0;
  int word = 0;
  for (i = 0 ; str[i] != '\0' ; i ++)
  {
    if (str[i] == ' ')
    {
      word = 0;
    }else if (word == 0)
    {
      word = 1;
      count ++;
    }
  }
  return count;
}

字符大寫轉(zhuǎn)小寫

1
2
3
4
5
6
7
8
9
10
void mylwr(char str[]){
  int i;
  for (i = 0 ; str[i] != '\0' ; i ++)
  {
    if (str[i] >= 'A' && str[i] <= 'Z')
    {
      str[i] += 'a' - 'A';
    }
  }
}

字符小寫轉(zhuǎn)大寫

1
2
3
4
5
6
7
8
9
10
void myupr(char str[]){
  int i;
  for (i = 0 ; str[i] != '\0' ; i ++)
  {
    if (str[i] >= 'a' && str[i] <= 'z')
    {
      str[i] -= 'a' - 'A';
    }
  }
}

字符數(shù)組計(jì)算字符串長度   

1
2
3
4
5
int mylen(char str[]){
  int len;
  for (len = 0 ; str[len] != '\0' ; len ++);
  return len;
}

字符串連接 

1
2
3
4
5
6
7
8
9
void mycat(char str1[],char str2[]){
  int i,j;
  for (i = 0 ; str1[i] != '\0' ;i++);
  for (j = 0 ; str2[j] != '\0' ; j ++)
  {
    str1[i + j] = str2[j];
  }
  str1[i + j] = '\0';
}

指定長度串接 

1
2
3
4
5
6
7
8
9
void myncat(char str1[],char str2[], int len){
   int i,j;
   for(i = 0; str1[i] != '\0'; i++);
   for (j = 0; j < len; j++)
   {
     str1[i + j] = str2[j];
   }
   str1[i + j] = '\0';
 }

字符數(shù)組拷貝     

1
2
3
4
5
6
7
void mycpy(char dst[],char src[]){
  int i = 0;
  do 
  {
    dst[i] = src[i];
  } while (src[i++] != '\0');
}

字符數(shù)組指定長度拷貝     

1
2
3
4
5
6
7
8
void myncpy(char dst[],char src[], int len){
  int i;
  for (i = 0; i < len; i++)
  {
    dst[i] = src[i];
  }
  dst[i] = '\0';
}

找出句子中最長的單詞 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void longest(char dst[],char src[]){
  int i = 0,j;
  int count =0;
  int max = -1;
  do 
  {
    if (src[i] ==' ' || src[i] == '\0')
    {
      if (count > max)
      {
        max = count;
        for (j = 0; j < count; j++)
        {
          dst[j] = src[i - count + j];
        }
        dst[j] = '\0';
      }
      count = 0;
    }else{
      count++;
    }
  } while (src[i++] != '\0');
}

從字符串中提取整形數(shù)字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
  
int getint(char str[], int a[]){//從字符串中提取數(shù)字并放在數(shù)組中
  int i = 0;
  int w = 0;
  int c = 0;
  int j, k;
  do 
  {
    if (str[i] > '0' && str[i] <= '9')
    {
      w++;
    }else if (w)
    {
      j = 0;
      for (k = w; k > 0; k--)
      {
        j *= 10;
        j += str[i - k] - '0';
      }
      w = 0;
      a[c] = j;
      c++;
    }
  } while (str[i++] != '\0');
  return c;
}
  
void main(){
  char str[100];
  int a[100];
  int i, j;
  gets(str);
  i = getint(str,a);
  for (j = 0; j < i; j++)
  {
    printf("%d ",a[j]);
  }
}

整形、字符數(shù)組型轉(zhuǎn)換

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdlib.h>
  
int sumof1(int x)//求一個(gè)數(shù)轉(zhuǎn)換成二進(jìn)制以后1的個(gè)數(shù)
{
  int countx = 0;
  while(x)
  {
    countx ++;
    x &= x-1; //每位與一次x - 1;就能消掉最后一個(gè)1
  }
  return countx;
}
  
void main(){
  
  char c[10];
  int i = 999;
  
  itoa(i, c, 10);//以10進(jìn)制轉(zhuǎn)換成字符數(shù)組
  puts(c);
  
  itoa(i, c, 16);//以16進(jìn)制轉(zhuǎn)換成字符數(shù)組
  printf("0x%s\n", c);
  
  itoa(i, c, 8);//以8進(jìn)制轉(zhuǎn)換成字符數(shù)組
  printf("0%s\n", c);
  
  itoa(i, c, 2);//以2進(jìn)制轉(zhuǎn)換成字符數(shù)組
  puts(c);
  
  i = atoi(c);//再將字符串轉(zhuǎn)成整形
  printf("%d\n", i);
  
  printf("%d\n", sumof1(i));//以2進(jìn)制表示時(shí)1的個(gè)數(shù)
}

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
字符串中查找字符(yc)
C語言的那些小秘密之指針(三)
C語言cgi程序在apache上的實(shí)現(xiàn)
深入理解C語言
C語言常用的庫函數(shù)
【編程練習(xí)】字符串循環(huán)右移
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服