编程札记
C实例 斐波那契系列
C实例 斐波那契系列 #include <stdio.h> int main() { int a, b, c, i, n; n = 4; a = b = 1; printf("%d %d ",a,b); for(i = 1; i <= n-2; i++) { c = a + b; printf("%d ", c); a = b; b = c; } return 0; }
阅读全文 ↗C示例查找Prime编号的程序
C示例查找Prime编号的程序 #include <stdio.h> int main() { int loop, number; int prime = 1; number = 11; for(loop = 2; loop < number; loop++) { if((number % loop) == 0) { prime = 0; } } if (prime == 1) printf("%d is prime number.", number); else printf("%d is not a prime number.", number); return 0; }
阅读全文 ↗C示例阿姆斯壮数字计划
C示例阿姆斯壮数字计划 #include <stdio.h> int main() { int arms = 153; int check, rem, sum = 0; check = arms; while(check != 0) { rem = check % 10; sum = sum + (rem * rem * rem); check = check / 10; } if(sum == arms) printf("%d is an armstrong number.", arms); else printf("%d is not an armstrong number.", arms); return 0; }
阅读全文 ↗C示例反转一行的程序
C示例反转一行的程序 #include <stdio.h> #include <string.h> int string_length(char s[]) { int i = 0; while(s[i]!='\0') i++; return i; } void string_reverse(char st[]) { int i,j,len; char ch; j = len = string_length(st) - 1; i = 0; while(i < j) { ch = st[j]; st[j] = st[i]; st[i] = ch; i++; j--; } } int main (void) { char line[] = "Taj Mahal is one of the seven wonders of the world"; char reverse[100] = "", temp[50]; int i, j, n; n = string_length(line); for(i = n-1; i >= 0; --i) { for(j = 0; i >= 0 && line[i] != ' '; --i,++j) temp[j] = line[i]; temp[j] = '\0'; string_reverse(temp); strcat(reverse,temp); strcat(reverse," "); } printf("Original - %s\n", line); printf("Reversed - %s\n",reverse); return 0; }
阅读全文 ↗C示例用于反转一行中的单词的程序
C示例用于反转一行中的单词的程序 #include <stdio.h> #include <string.h> int string_length(char s[]) { int i = 0; while(s[i]!='\0') i++; return i; } void string_reverse(char st[]) { int i,j,len; char ch; j = len = string_length(st) - 1; i = 0; while(i < j) { ch = st[j]; st[j] = st[i]; st[i] = ch; i++; j--; } } int main (void) { char line[] = "Taj Mahal is one of the seven wonders of the world"; char reverse[100]="",temp[50]; int i,j,n; n = string_length(line); for(i = 0; i < n; i++) { for(j = 0; i < n && line[i]!=' '; ++i,++j) { temp[j] = line[i]; } temp[j] = '\0'; string_reverse(temp); strcat(reverse, temp); strcat(reverse, " "); } printf("Original - %s\n", line); printf("Reversed - %s\n",reverse); return 0; }
阅读全文 ↗C实例 字符串字谜
C实例 字符串字谜 #include <stdio.h> #include <string.h> int main (void) { char s1[] = "recitals"; char s2[] = "articles"; char temp; int i, j; int n = strlen(s1); int n1 = strlen(s2); // If both strings are of different length, then they are not anagrams if( n != n1) { printf("%s and %s are not anagrams! \n", s1, s2); return 0; } // lets sort both strings first − for (i = 0; i < n-1; i++) { for (j = i+1; j < n; j++) { if (s1[i] > s1[j]) { temp = s1[i]; s1[i] = s1[j]; s1[j] = temp; } if (s2[i] > s2[j]) { temp = s2[i]; s2[i] = s2[j]; s2[j] = temp; } } } // Compare both strings character by character for(i = 0; i<n; i++) { if(s1[i] != s2[i]) { printf("Strings are not anagrams! \n", s1, s2); return 0; } } printf("Strings are anagrams! \n"); return 0; }
阅读全文 ↗C实例 连接两个字符串
C实例 连接两个字符串 #include <stdio.h> #include <string.h> int main() { char s1[10] = "Taj"; char s2[] = "Mahal"; int i, j, n1, n2; n1 = strlen(s1); n2 = strlen(s2); j = 0; for(i = n1; i< n1+n2; i++ ) { s1[i] = s2[j]; j++; } s1[i] = '\0'; printf("%s", s1); return 0; }
阅读全文 ↗C实例 比较字符串
C实例 比较字符串 #include <stdio.h> int main() { char s1[] = "advise"; char s2[] = "advice"; int n = 0; unsigned short flag = 1; while (s1[n] != '\0') { if(s1[n] != s2[n]) { flag = 0; break; } n++; } if(flag == 1) { printf("%s and %s are identical\n", s1, s2); } else { printf("%s and %s are NOT identical\n", s1, s2); } return 0; }
阅读全文 ↗C实例 交互字符串
C实例 交互字符串 #include <stdio.h> int main() { char s1[] = "TajMahal"; char s2[] = "Dazzling"; char ch; int index = 0; //Character by Character approach printf("Before Swapping - \n"); printf("Value of s1 - %s \n", s1); printf("Value of s2 - %s \n", s2); while(s1[index] != '\0') { ch = s1[index]; s1[index] = s2[index]; s2[index] = ch; index++; } printf("After Swapping - \n"); printf("Value of s1 - %s \n", s1); printf("Value of s2 - %s \n", s2); return 0; }
阅读全文 ↗