编程札记

C

C示例程序搜索字符串

C示例程序搜索字符串 #include <stdio.h> #include <string.h> int main() { char s1[] = "Beauty is in the eye of the beholder"; char s2[] = "the"; int n = 0; int m = 0; int times = 0; int len = strlen(s2); // contains the length of search string while(s1[n] != '\0') { if(s1[n] == s2[m]) { // if first character of search string matches // keep on searching while(s1[n] == s2[m] && s1[n] !='\0') { n++; m++; } // if we sequence of characters matching with the length of searched string if(m == len && (s1[n] == ' ' || s1[n] == '\0')) { // BINGO!! we find our search string. times++; } } else { // if first character of search string DOES NOT match while(s1[n] != ' ') { // Skip to next word n++; if(s1[n] == '\0') break; } } n++; m=0; // reset the counter to start from first character of the search string. } if(times > 0) { printf("'%s' appears %d time(s)\n", s2, times); } else { printf("'%s' does not appear in the sentence.\n", s2); } return 0; }

阅读全文 ↗
C

C示例用于反转String的程序

C示例用于反转String的程序 #include <stdio.h> int main() { char s1[] = "TajMahal"; // String Given char s2[8]; // Variable to store reverse string int length = 0; int loop = 0; while(s1[length] != '\0') { length++; } printf("\nPrinting in reverse - "); for(loop = --length; loop>=0; loop--) printf("%c", s1[loop]); loop = 0; printf("\nStoring in reverse - "); while(length >= 0) { s2[length] = s1[loop]; length--; loop++; } s1[loop] = '\0'; // Terminates the string printf("%s\n", s2); return 0; }

阅读全文 ↗
C

C示例复制字符串的程序

C示例复制字符串的程序 #include <stdio.h> int main() { char s1[] = "TajMahal"; // String Given char s2[8]; // Variable to hold value int length = 0; while(s1[length] != '\0') { s2[length] = s1[length]; length++; } s2[length] = '\0'; // Terminate the string printf("Value in s1 = %s \n", s1); printf("Value in s2 = %s \n", s2); return 0; }

阅读全文 ↗
C

C示例排序字符串字符的程序

C示例排序字符串字符的程序 #include <stdio.h> #include <string.h> int main (void) { char string[] = "simplyeasylearning"; char temp; int i, j; int n = strlen(string); printf("String before sorting - %s \n", string); for (i = 0; i < n-1; i++) { for (j = i+1; j < n; j++) { if (string[i] > string[j]) { temp = string[i]; string[i] = string[j]; string[j] = temp; } } } printf("String after sorting - %s \n", string); return 0; }

阅读全文 ↗
C

C示例计算元音的程序

C示例计算元音的程序 #include <stdio.h> int main() { char s[] = "TajMahal"; // String Given int i = 0; int vowels = 0; // Vowels counter int consonants = 0; // Consonants counter while(s[i++] != '\0') { if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' ) vowels++; else consonants++; } printf("'%s' contains %d vowels and %d consonants.", s, vowels, consonants); return 0; }

阅读全文 ↗
C

C实例 计算字符

C实例 计算字符 #include <stdio.h> int main() { char s[] = "TajMahal"; // String Given char ch = 'a'; // Character to count int i = 0; int count = 0; // Counter while(s[i] != '\0') { if(s[i] == ch) count++; i++; } if(count > 0) { if(count == 1) printf("%c appears %d time in '%s'", ch, count, s); else printf("%c appears %d times in '%s'", ch, count, s); } else printf("%c did not appear in %s", ch, s); return 0; }

阅读全文 ↗
C

C实例 连接数组

C实例 连接数组 #include <stdio.h> int main() { int array[10]; int even[5] = {0, 2, 4, 6, 8}; int odd[5] = {1, 3, 5, 7, 9}; int loop, index, e_len, o_len; e_len = o_len = 5; index = 0; for(loop = 0; loop < e_len; loop++) { array[index] = even[loop]; index++; } for(loop = 0; loop < o_len; loop++) { array[index] = odd[loop]; index++; } printf("\nEven -> "); for(loop = 0; loop < e_len; loop++) printf(" %d", even[loop]); printf("\nOdd -> "); for(loop = 0; loop < o_len; loop++) printf(" %d", odd[loop]); printf("\nConcat -> "); for(loop = 0; loop < 10; loop++) printf(" %d", array[loop]); return 0; }

阅读全文 ↗
C

C示例分割数组的程序

C示例分割数组的程序 #include <stdio.h> int main() { int array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int even[10], odd[10]; int loop, e, d; e = d = 0; for(loop = 0; loop < 10; loop++) { if(array[loop]%2 == 0) { even[e] = array[loop]; e++; } else { odd[d] = array[loop]; d++; } } printf(" original -> "); for(loop = 0; loop < 10; loop++) printf(" %d", array[loop]); printf("\n even -> "); for(loop = 0; loop < e; loop++) printf(" %d", even[loop]); printf("\n odd -> "); for(loop = 0; loop < d; loop++) printf(" %d", odd[loop]); return 0; }

阅读全文 ↗
C

C实例 逆序拷贝数组

C实例 逆序拷贝数组 #include <stdio.h> int main() { int original[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; int copied[10]; int loop, count; count = 9; for(loop = 0; loop < 10; loop++) { copied[count] = original[loop]; count--; } printf("original -> copied \n"); for(loop = 0; loop < 10; loop++) { printf(" %2d %2d\n", original[loop], copied[loop]); } return 0; }

阅读全文 ↗