C语言字符数组基本操作

test

  1 #include <stdio.h>
  2 
  3 /*
  4 *字符数组倒序
  5 */
  6 void daoxu(char str[]){
  7     int i;
  8     char temp;
  9     for(i = 0; i < strlen(str) / 2 ; i ++){
 10         temp = str[i];
 11         str[i] = str[strlen(str) - i-1];
 12         str[strlen(str) - i-1] = temp;
 13     }
 14 }
 15 
 16 /*
 17 *单词计数
 18 */
 19 int wordCount(char str[]){
 20     int i;
 21     int count = 0;
 22     int word = 0;
 23     for (i = 0 ; str[i] != '\0' ; i ++)
 24     {
 25         if (str[i] == ' ')
 26         {
 27             word = 0;
 28         }else if (word == 0)
 29         {
 30             word = 1;
 31             count ++;
 32         }
 33     }
 34     return count;
 35 }
 36 
 37 /*
 38 *字符大写转小写
 39 */
 40 void mylwr(char str[]){
 41     int i;
 42     for (i = 0 ; str[i] != '\0' ; i ++)
 43     {
 44         if (str[i] >= 'A' && str[i] <= 'Z')
 45         {
 46             str[i] += 'a' - 'A';
 47         }
 48     }
 49 }
 50 
 51 /*
 52 *字符小写转大写
 53 */
 54 void myupr(char str[]){
 55     int i;
 56     for (i = 0 ; str[i] != '\0' ; i ++)
 57     {
 58         if (str[i] >= 'a' && str[i] <= 'z')
 59         {
 60             str[i] -= 'a' - 'A';
 61         }
 62     }
 63 }
 64 
 65 /*
 66 *字符数组计算字符串长度
 67 */
 68 int mylen(char str[]){
 69     int len;
 70     for (len = 0 ; str[len] != '\0' ; len ++);
 71     return len;
 72 }
 73 
 74 /*
 75 *字符串连接
 76 */
 77 void mycat(char str1[],char str2[]){
 78     int i,j;
 79     for (i = 0 ; str1[i] != '\0' ;i++);
 80     for (j = 0 ; str2[j] != '\0' ; j ++)
 81     {
 82         str1[i + j] = str2[j];
 83     }
 84     str1[i + j] = '\0';
 85 }
 86 
 87 /*
 88 *指定长度串接
 89 */
 90 void myncat(char str1[],char str2[], int len){
 91     int i,j;
 92     for(i = 0; str1[i] != '\0'; i++);
 93     for (j = 0; j < len; j++)
 94     {
 95         str1[i + j] = str2[j];
 96     }
 97     str1[i + j] = '\0';
 98 }
 99 
100 /*
101 *字符数组拷贝    
102 */
103 void mycpy(char dst[],char src[]){
104     int i = 0;
105     do 
106     {
107         dst[i] = src[i];
108     } while (src[i++] != '\0');
109 }
110 
111 /*
112 *字符数组指定长度拷贝    
113 */
114 void myncpy(char dst[],char src[], int len){
115     int i;
116     for (i = 0; i < len; i++)
117     {
118         dst[i] = src[i];
119     }
120     dst[i] = '\0';
121 }
122 
123 /*
124 *找出句子中最长的单词
125 */
126 void longest(char dst[],char src[]){
127     int i = 0,j;
128     int count =0;
129     int max = -1;
130     do 
131     {
132         if (src[i] ==' ' || src[i] == '\0')
133         {
134             if (count > max)
135             {
136                 max = count;
137                 for (j = 0; j < count; j++)
138                 {
139                     dst[j] = src[i - count + j];
140                 }
141                 dst[j] = '\0';
142             }
143             count = 0;
144         }else{
145             count++;
146         }
147     } while (src[i++] != '\0');
148 }
149 
150 void main(){
151     char str[100];
152     char str2[100];
153     //gets(str);
154     gets(str2);
155     //daoxu(str);
156     //mylwr(str);
157     //myupr(str);
158     //puts(str);
159     //printf("%d\n",wordCount(str));
160     //printf("%d\n",mylen(str));
161     //mycpy(str,str2);
162     //myncpy(str,str2,5);
163     longest(str,str2);
164     puts(str);
165 }

 

example

posted @ 2012-07-30 22:27  丛林听雨  阅读(589)  评论(0编辑  收藏  举报