Vous êtes sur la page 1sur 6

ASCII Code – A to Z == 65 to 90 && a to z == 97 to 92

File headers: #include <stdio.h> <stdlib.h> <string.h>

int i,k,len[15], temp; // string declarations


char strings[max][max], tempstr[max],lwrcase[20];
sortout(strings, len); // function for sort.out
void sortout(char strings[][max], int len[]) // function for sort.out FILE

FILE *f1; // open file


f1 = fopen("strings.in","r");
if (f1 == NULL) {
printf("No file detected");
exit; }

// read strings from file1


and // identify the length of each strings
for (i = 0; i <= 10; i++) {
fscanf(f1,"%s", strings[i]);
len[i] = strlen(strings[i]); }

Binary Search
1. #include <stdio.h>
2. int main()
3. {
4. int c, first, last, middle, n, search, array[100];
5.
6. printf("Enter number of elements\n");
7. scanf("%d",&n);
8.
9. printf("Enter %d integers\n", n);
10.
11. for (c = 0; c < n; c++)
12. scanf("%d",&array[c]);
13.
14. printf("Enter value to find\n");
15. scanf("%d", &search);
16.
17. first = 0;
18. last = n - 1;
19. middle = (first+last)/2;
20.
21. while (first <= last) {
22. if (array[middle] < search)
23. first = middle + 1;
24. else if (array[middle] == search) {
25. printf("%d found at location
%d.\n", search, middle+1);
26. break;
27. }
28. else
29. last = middle - 1;
30.
31. middle = (first + last)/2;
32. }
33. if (first > last)
34. printf("Not found! %d isn't present in the
list.\n", search);
35.
36. return 0;
37. }

Linear Search

1. #include <stdio.h>
2.
3. int main()
4. {
5. int array[100], search, c, n;
6.
7. printf("Enter number of elements in array\n");
8. scanf("%d", &n);
9.
10. printf("Enter %d integer(s)\n", n);
11.
12. for (c = 0; c < n; c++)
13. scanf("%d", &array[c]);
14.
15. printf("Enter a number to search\n");
16. scanf("%d", &search);
17.
18. for (c = 0; c < n; c++)
19. {
20. if (array[c] == search) /* If required element is found
*/
21. {
22. printf("%d is present at location %d.\n", search, c+1);
23. break;
24. }
25. }
26. if (c == n)
27. printf("%d isn't present in the array.\n", search);
28.
29. return 0;
30. }

Selection sort

1. #include <stdio.h>
2.
3. int main()
4. {
5. int array[100], n, c, d, position, swap;
6.
7. printf("Enter number of elements\n");
8. scanf("%d", &n);
9.
10. printf("Enter %d integers\n", n);
11.
12. for (c = 0; c < n; c++)
13. scanf("%d", &array[c]);
14.
15. for (c = 0; c < (n - 1); c++)
16. {
17. position = c;
18.
19. for (d = c + 1; d < n; d++)
20. {
21. if (array[position] > array[d])
22. position = d;
23. }
24. if (position != c)
25. {
26. swap = array[c];
27. array[c] = array[position];
28. array[position] = swap;
29. }
30. }
31.
32. printf("Sorted list in ascending order:\n");
33.
34. for (c = 0; c < n; c++)
35. printf("%d\n", array[c]);
36.
37. return 0;
38. }
To print the ascii value

39. #include <stdio.h>


40. int main()
41. {
42. char c;
43. printf("Enter a character: ");
44.
45. // Reads character input from the user
46. scanf("%c", &c);
47.
48. // %d displays the integer value of a character
49. // %c displays the actual character
50. printf("ASCII value of %c = %d", c, c);
51. return 0;
52. }

Remove all not alphabets

#include<stdio.h>
int main() {
char line[150];
int i, j;
printf("Enter a string: ");
gets(line);
for(i = 0; line[i] != '\0'; ++i) {
while (!( (line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' &&
line[i] <= 'Z') || line[i] == '\0') ) {
for(j = i; line[j] != '\0'; ++j) {
line[j] = line[j+1];
}
line[j] = '\0';
} }
printf("Output String: ");
puts(line);
return 0; }
strcasecmp(); strcat(); strcpy; strcmp; strlen;

if ((fptr = fopen("program.txt", "r")) == NULL)


{
printf("Error! opening file");
// Program exits if file pointer returns NULL.
exit(1);
}
// reads text until newline
fscanf(fptr,"%[^\n]", c);

// C program to print reverse of words in


// a string.
#include <stdio.h>
#include <string.h>

void printReverse(char str[])


{
int length = strlen(str);

// Traverse string from end


int i;
for (i = length - 1; i >= 0; i--) {
if (str[i] == ' ') {

// putting the NULL character at the


// position of space characters for
// next iteration.
str[i] = '\0';

// Start from next charatcer


printf("%s ", &(str[i]) + 1);
}
}

// printing the last word


printf("%s", str);
}

// Driver code
int main()
{
char str[] = "I AM A GEEK";
printReverse(str);
return 0;
}
/* Example to reverse a sentence entered by user without using strings. */
#include <stdio.h>
void reverseSentence();
int main() {
printf("Enter a sentence: ");
reverseSentence();
return 0;
}
void reverseSentence() {
char c;
scanf("%c", &c);
if( c != '\n') {
reverseSentence();
printf("%c",c); }
}

// C program to reverse string if (str[wend] == ' ')


// according to the number of words continue;
#include<stdio.h> // Checking the number of
#include<string.h> words
// Reverse the letters of the word // present in string to
void reverse(char str[], int start, reverse
int end) { while (str[wend] != ' ' &&
// Temporary variable to store wend <= end)
character wend++;
char temp; wend--;
while (start <= end) {
// Swapping the first and //Reverse the letter
last character //of the words
temp = str[start]; reverse(str, wstart, wend);
str[start] = str[end]; }
str[end] = temp; }
start++;
end--;
// Driver Code
}
int main()
} {
// This function forms the required char str[1000] = "Ashish Yadav
string Abhishek Rajput Sunil Pundir";
void reverseletter(char str[], int reverseletter(str, 0,
start, int end) { strlen(str)-1);
int wstart, wend; printf("%s", str);
for (wstart = wend = start; return 0;
wend < end; wend++) { }

Vous aimerez peut-être aussi