Codes

// Finding string length using strlen

#include <stdio.h>

#include <string.h>


int main()

{

    char a[50];

    int l;


    printf("Enter a string: ");

    gets(a);


    l = strlen(a);

    printf("Length of string is: %d", l);


    return 0;

}

c

Copy code

// Copying a string using strcpy

#include <stdio.h>

#include <string.h>


int main()

{

    char str1[50], str2[50];


    printf("Enter string: ");

    gets(str1);


    strcpy(str2, str1);

    puts(str2);


    return 0;

}

c

Copy code

// Comparing strings using strcmp

#include <stdio.h>

#include <string.h>


int main()

{

    char str1[50], str2[50];

    int c;


    printf("Enter first string: ");

    gets(str1);


    printf("Enter second string: ");

    gets(str2);


    c = strcmp(str1, str2);


    if (c == 0)

        printf("Strings are equal");

    else

        printf("Strings are not equal");


    return 0;

}

c

Copy code

// Concatenation of strings using strcat

#include <stdio.h>

#include <string.h>


int main()

{

    char str1[50], str2[50];


    printf("Enter first string: ");

    gets(str1);


    printf("Enter second string: ");

    gets(str2);


    strcat(str1, str2);

    puts(str1);


    return 0;

}





// String reverse

#include <stdio.h>

#include <string.h>


int main()

{

    char str[50];


    printf("Enter string: ");

    gets(str);


    strrev(str);

    puts(str);


    return 0;

}

2. Finding String Length (User-defined function)

c

Copy code

// Finding string length

#include <stdio.h>

#include <string.h>


int mystrlen(char []);


int main()

{

    char str[50];

    int length;


    printf("Enter a string: ");

    gets(str);


    length = mystrlen(str);

    printf("%d", length);


    return 0;

}


int mystrlen(char str[50])

{

    int i = 0;


    while (str[i] != '\0')

    {

        i++;

    }


    return i;

}

3. String Copying (User-defined function)

c

Copy code

// String copying

#include <stdio.h>

#include <string.h>


void mystrcpy(char str2[50], char str1[50]);


int main()

{

    char str1[50], str2[50];


    printf("Enter string: ");

    gets(str1);


    mystrcpy(str2, str1);

    puts(str2);


    return 0;

}


void mystrcpy(char str2[50], char str1[50])

{

    int s = 0;


    while (str1[s] != '\0')

    {

        str2[s] = str1[s];

        s++;

    }


    str2[s] = '\0';

}


1. String Comparison (User-defined strcmp)

// String comparison #include <stdio.h> #include <string.h> int mystrcmp(char [], char []); int main() { char str1[50], str2[50]; int c; printf("Enter first string: "); gets(str1); printf("Enter second string: "); gets(str2); c = mystrcmp(str1, str2); if (c == 0) printf("Strings are equal"); else printf("Strings are not equal"); return 0; } // Function definition int mystrcmp(char str1[50], char str2[50]) { int i = 0; while (str1[i] != '\0' || str2[i] != '\0') { if (str1[i] > str2[i]) return 1; else if (str1[i] < str2[i]) return -1; i++; } return 0; }

2. String Concatenation (User-defined strcat)

// String concatenation #include <stdio.h> #include <string.h> void mystrcat(char str1[50], char str2[50]); int main() { char str1[50], str2[50]; printf("Enter first string: "); gets(str1); printf("Enter second string: "); gets(str2); mystrcat(str1, str2); puts(str1); return 0; } void mystrcat(char str1[50], char str2[50]) { int i, l1, l2; l1 = strlen(str1); l2 = strlen(str2); for (i = 0; i < l2; i++) { str1[l1 + i] = str2[i]; } str1[l1 + l2] = '\0'; }

3. String Reverse (User-defined Function)

// String reverse #include <stdio.h> #include <string.h> void mystrrev(char str[50]); int main() { char str[50]; printf("Enter string: "); gets(str); mystrrev(str); printf("Reverse of entered string is: "); puts(str); return 0; } void mystrrev(char str[50]) { char temp; int i, j, l; l = strlen(str); for (i = 0, j = l - 1; i <= j; i++, j--) { temp = str[i]; str[i] = str[j]; str[j] = temp; } }

4. Use of fflush(stdin)

// Use of fflush(stdin) #include <stdio.h> int main() { char str[50]; int i; printf("Enter vowels: "); for (i = 0; i < 5; i++) { fflush(stdin); scanf("%c", &str[i]); } for (i = 0; i < 5; i++) { printf("%c", str[i]); } return 0; }

5. Palindrome Check Using String Functions

/* Write a C program to check whether the entered string is palindrome or not */ #include <stdio.h> #include <string.h> void mystrrev(char [], char []); int mystrcmp(char [], char []); int main() { char str1[50], str2[50]; int c; printf("Enter a string: "); gets(str1); mystrrev(str2, str1); c = mystrcmp(str1, str2); if (c == 0) printf("Entered string is a palindrome"); else printf("Entered string is not a palindrome"); return 0; }

String Reverse (Using Another String)

void mystrrev(char str2[50], char str1[50]) { int i, l; l = strlen(str1) - 1; for (i = 0; i <= l; i++) { str2[i] = str1[l - i]; } str2[i] = '\0'; }

String Comparison (User-defined strcmp)

int mystrcmp(char str1[50], char str2[50]) { int i = 0; while (str1[i] != '\0' || str2[i] != '\0') { if (str1[i] > str2[i]) return 1; else if (str1[i] < str2[i]) return -1; i++; } return 0; }

Toggle / Invert Case of String

#include <stdio.h> #include <string.h> int main() { char str[80]; int i = 0; printf("Enter a string: "); gets(str); while (str[i] != '\0') { if (str[i] >= 'A' && str[i] <= 'Z') str[i] = str[i] + 32; else if (str[i] >= 'a' && str[i] <= 'z') str[i] = str[i] - 32; i++; } printf("Toggled string is "); puts(str); return 0; }

Count Vowels and Consonants

#include <stdio.h> #include <string.h> int main() { char str[50]; int i = 0, vow = 0, con = 0; printf("Enter a string: "); gets(str); while (str[i] != '\0') { if (str[i] == 'a' || str[i] == 'A' || str[i] == 'e' || str[i] == 'E' || str[i] == 'i' || str[i] == 'I' || str[i] == 'o' || str[i] == 'O' || str[i] == 'u' || str[i] == 'U') vow++; else con++; i++; } printf("No. of vowels: %d", vow); printf("\nNo. of consonants: %d", con); return 0; }

Check Character Type

#include <stdio.h> #include <string.h> int main() { char str[50]; int i = 0; printf("Enter a string: "); gets(str); while (str[i] != '\0') { if (str[i] >= 'A' && str[i] <= 'Z') printf("\n%c is a capital letter", str[i]); else if (str[i] >= 'a' && str[i] <= 'z') printf("\n%c is a small letter", str[i]); else if (str[i] >= '0' && str[i] <= '9') printf("\n%c is a digit", str[i]); else printf("\n%c is a special symbol", str[i]); i++; } return 0; }

POINTERS

Swap Two Numbers Using Pointers

#include <stdio.h> void swap(int *, int *); int main() { int num1, num2; printf("Enter 2 numbers: "); scanf("%d%d", &num1, &num2); printf("Numbers before swapping: %d, %d", num1, num2); swap(&num1, &num2); printf("\nNumbers after swapping: %d, %d", num1, num2); return 0; } void swap(int *num1, int *num2) { int temp; temp = *num1; *num1 = *num2; *num2 = temp; }

Area of Triangle (Call by Reference)

#include <stdio.h> #include <math.h> void area(int, int, int, float *); int main() { int a, b, c; float ar = 0.0; printf("Enter values of a, b and c: "); scanf("%d%d%d", &a, &b, &c); area(a, b, c, &ar); printf("The area of triangle is: %.2f", ar); return 0; } void area(int a, int b, int c, float *ar) { int s; s = (a + b + c) / 2; if (a + b > c && b + c > a && a + c > b) *ar = sqrt(s * (s - a) * (s - b) * (s - c)); else printf("Invalid triangle"); }

Reverse Array Using Pointer

#include <stdio.h> int main() { int arr[50], i, n; int *p = arr; printf("Enter range: "); scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &arr[i]); p = &arr[n - 1]; printf("Reverse: "); for (i = 0; i < n; i++) printf("%d ", *(p--)); return 0; }

Average of Array Using Pointer

#include <stdio.h> float average(int *, int); int main() { int arr[50], n, i; int *p = arr; printf("Enter size of array: "); scanf("%d", &n); printf("Enter elements:\n"); for (i = 0; i < n; i++) scanf("%d", &arr[i]); printf("Average: %.2f", average(p, n)); return 0; } float average(int *p, int n) { int i, sum = 0; for (i = 0; i < n; i++) sum += *(p++); return (float)sum / n; }

Reverse Array Using Pointer & Function

#include <stdio.h> void reverse(int *, int); int main() { int arr[50], n, i; int *ap = arr; printf("Enter number of elements: "); scanf("%d", &n); printf("Enter elements: "); for (i = 0; i < n; i++) scanf("%d", ap + i); reverse(ap, n); printf("Reversed array:"); for (i = 0; i < n; i++) printf("\n%d", *(ap + i)); return 0; } void reverse(int *ap, int n) { int i, j, temp; for (i = 0, j = n - 1; i < j; i++, j--) { temp = *(ap + i); *(ap + i) = *(ap + j); *(ap + j) = temp; } }

Armstrong Number Using Pointer

#include <stdio.h> #include <math.h> int armstrong(int *); int main() { int n, *p, temp, sum; p = &n; printf("Enter a number: "); scanf("%d", &n); temp = n; sum = armstrong(p); if (sum == temp) printf("Entered number is an Armstrong number"); else printf("Entered number is not an Armstrong number"); return 0; } int armstrong(int *p) { int rem, sum = 0; while (*p != 0) { rem = (*p) % 10; sum += pow(rem, 3); *p = (*p) / 10; } return sum; }

POINTER AND STRING

1. Finding String Length Using Pointer

/* Finding string length using pointer */ #include <stdio.h> #include <string.h> int mystrlen(char *); int main() { char str[50], *s; int length; printf("Enter a string: "); gets(str); s = str; length = mystrlen(s); printf("%d", length); return 0; } int mystrlen(char *s) { int i = 0; while (*s != '\0') { i++; s++; } return i; }

2. String Comparison Using Pointer

/* String comparison using pointer */ #include <stdio.h> #include <string.h> int mystrcmp(char *, char *); int main() { char str1[50], str2[50]; int c; printf("Enter first string: "); gets(str1); printf("Enter second string: "); gets(str2); c = mystrcmp(str1, str2); if (c == 0) printf("Strings are equal"); else printf("Strings are not equal"); return 0; } int mystrcmp(char *p1, char *p2) { while (*p1 != '\0' || *p2 != '\0') { if (*p1 > *p2) return 1; else if (*p1 < *p2) return -1; p1++; p2++; } return 0; }

STRUCTURES

3. Structure Declaration & Initialization

#include <stdio.h> #include <string.h> int main() { struct info { int sno; char name[20]; char add[20]; } s1, s2; struct info stu1 = {1, "ajay", "dehradun"}; struct info stu2, stu3; stu2.sno = 2; strcpy(stu2.name, "rashmi"); strcpy(stu2.add, "delhi"); printf("Enter sno, name and address of third student:\n"); scanf("%d%s%s", &stu3.sno, stu3.name, stu3.add); printf("\nDetails of students:\n"); printf("\n1st: %d %s %s", stu1.sno, stu1.name, stu1.add); printf("\n2nd: %d %s %s", stu2.sno, stu2.name, stu2.add); printf("\n3rd: %d %s %s", stu3.sno, stu3.name, stu3.add); return 0; }

4. Student Information (Single Student)

#include <stdio.h> int main() { typedef struct { int sno; char name[20]; } student; student s1; printf("Enter sno and name: "); scanf("%d%s", &s1.sno, s1.name); printf("Sno: %d Name: %s\n", s1.sno, s1.name); return 0; }

5. Student Marks in 4 Subjects

#include <stdio.h> int main() { typedef struct { int sno; char name[20]; int submarks[4]; } student; student s1; int j; printf("Enter sno and name: "); scanf("%d%s", &s1.sno, s1.name); printf("Enter marks in 4 subjects:\n"); for (j = 0; j < 4; j++) scanf("%d", &s1.submarks[j]); printf("Sno: %d Name: %s\n", s1.sno, s1.name); for (j = 0; j < 4; j++) printf("Subject %d marks: %d\n", j + 1, s1.submarks[j]); return 0; }

6. Information of N Students

#include <stdio.h> int main() { typedef struct { int sno; char name[20]; int submarks[4]; } student; student s[10]; int i, j, n; printf("Enter number of students: "); scanf("%d", &n); for (i = 0; i < n; i++) { printf("Enter sno and name: "); scanf("%d%s", &s[i].sno, s[i].name); printf("Enter marks in 4 subjects:\n"); for (j = 0; j < 4; j++) scanf("%d", &s[i].submarks[j]); } for (i = 0; i < n; i++) { printf("\nSno: %d Name: %s", s[i].sno, s[i].name); for (j = 0; j < 4; j++) printf("\nSubject %d marks: %d", j + 1, s[i].submarks[j]); } return 0; }

7. Average Percentage of Class

#include <stdio.h> #include <string.h> struct stu { char Name[15]; int Rn; float per; } s1[100]; int main() { int i, n; float sumper = 0, avgper; printf("Enter number of students: "); scanf("%d", &n); for (i = 0; i < n; i++) { fflush(stdin); printf("Name: "); gets(s1[i].Name); printf("Roll no: "); scanf("%d", &s1[i].Rn); printf("Percentage: "); scanf("%f", &s1[i].per); sumper += s1[i].per; } avgper = sumper / n; printf("Students scoring >= average:\n"); for (i = 0; i < n; i++) { if (s1[i].per >= avgper) { puts(s1[i].Name); printf("Roll no: %d\n", s1[i].Rn); } } return 0; }

8. Highest Percentage Student

#include <stdio.h> struct info { int rn; char name[20]; int marks[4]; float per; }; int main() { struct info s[50]; int n, i, j, total, index = 0; float high; printf("Enter number of students: "); scanf("%d", &n); for (i = 0; i < n; i++) { printf("Roll no: "); scanf("%d", &s[i].rn); fflush(stdin); printf("Name: "); gets(s[i].name); total = 0; printf("Enter marks:\n"); for (j = 0; j < 4; j++) { scanf("%d", &s[i].marks[j]); total += s[i].marks[j]; } s[i].per = (float)total / 4; } high = s[0].per; for (i = 1; i < n; i++) { if (s[i].per > high) { high = s[i].per; index = i; } } printf("Highest Percentage: %.2f\n", high); printf("Name: %s\nRoll no: %d\n", s[index].name, s[index].rn); return 0; }

9. Structure Pointer Example

#include <stdio.h> int main() { typedef struct { int rollno; char name[20]; } stud; stud s1, s2, *p1, *p2; p1 = &s1; p2 = &s2; printf("First Method:\n"); scanf("%d%s", &(*p2).rollno, (*p2).name); printf("Rollno: %d Name: %s\n", (*p2).rollno, (*p2).name); printf("\nSecond Method:\n"); scanf("%d%s", &(p1->rollno), p1->name); printf("Rollno: %d Name: %s\n", p1->rollno, p1->name); return 0; }

UNION

1. Union Example

/* Read and print information using union */ #include <stdio.h> #include <conio.h> int main() { union student { char grade; char name[20]; int roll; float per; } u1; printf("Enter the name:\n"); gets(u1.name); printf("Name -> %s\n", u1.name); printf("Enter the roll:\n"); scanf("%d", &u1.roll); printf("Roll no -> %d\n", u1.roll); printf("Enter the percentage:\n"); scanf("%f", &u1.per); printf("Percentage -> %f\n", u1.per); printf("Enter the grade:\n"); fflush(stdin); scanf("%c", &u1.grade); printf("Grade -> %c\n", u1.grade); /* Printing complete details (garbage values expected) */ printf("\nName -> %s", u1.name); printf("\nRoll -> %d", u1.roll); printf("\nPercentage -> %f", u1.per); printf("\nGrade -> %c", u1.grade); return 0; }

2. Memory Allocation: Structure vs Union

#include <stdio.h> #include <conio.h> int main() { typedef union { int roll; char grade; float per; } uni1; typedef struct { int roll; char grade; float per; } str1; uni1 u1; str1 s1; printf("\nSTRUCTURE"); printf("\nSize of structure: %d", sizeof(s1)); printf("\nAddress of s1: %u", &s1); printf("\nAddress of s1.roll: %u", &s1.roll); printf("\nAddress of s1.grade: %u", &s1.grade); printf("\nAddress of s1.per: %u", &s1.per); printf("\n\nUNION"); printf("\nSize of union: %d", sizeof(u1)); printf("\nAddress of u1: %u", &u1); printf("\nAddress of u1.roll: %u", &u1.roll); printf("\nAddress of u1.grade: %u", &u1.grade); printf("\nAddress of u1.per: %u", &u1.per); return 0; }

FILES

3. Store Characters in File & Read

#include <stdio.h> int main() { FILE *fp1; char ch; fp1 = fopen("Master.txt", "w"); if (fp1 == NULL) { printf("File creation error"); return 0; } printf("Enter characters:\n"); while ((ch = getchar()) != EOF) { fputc(ch, fp1); } fclose(fp1); fp1 = fopen("Master.txt", "r"); printf("File contents:\n"); while ((ch = fgetc(fp1)) != EOF) { putchar(ch); } fclose(fp1); return 0; }

4. Copy File Contents

#include <stdio.h> int main() { FILE *fp1, *fp2; char ch; fp1 = fopen("Master.txt", "w"); printf("Enter characters:\n"); while ((ch = getchar()) != EOF) putc(ch, fp1); fclose(fp1); fp1 = fopen("Master.txt", "r"); fp2 = fopen("copy.txt", "w"); while ((ch = getc(fp1)) != EOF) putc(ch, fp2); fclose(fp1); fclose(fp2); printf("Copied successfully"); return 0; }

5. Convert File Content to Uppercase

#include <stdio.h> #include <ctype.h> int main() { FILE *fp1, *fp2; char ch; fp1 = fopen("source.txt", "w"); printf("Enter characters:\n"); while ((ch = getchar()) != EOF) putc(ch, fp1); fclose(fp1); fp1 = fopen("source.txt", "r"); fp2 = fopen("copy_upper.txt", "w"); while ((ch = getc(fp1)) != EOF) { ch = toupper(ch); putc(ch, fp2); } fclose(fp1); fclose(fp2); return 0; }

6. Separate Vowels, Consonants, Digits & Special Characters

#include <stdio.h> int main() { FILE *fp1, *fv, *fc, *fd, *fs; char ch; fp1 = fopen("Master.txt", "w"); printf("Enter characters:\n"); while ((ch = getchar()) != EOF) putc(ch, fp1); fclose(fp1); fp1 = fopen("Master.txt", "r"); fv = fopen("vowel.txt", "w"); fc = fopen("consonant.txt", "w"); fd = fopen("digit.txt", "w"); fs = fopen("special.txt", "w"); while ((ch = getc(fp1)) != EOF) { if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'|| ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') putc(ch, fv); else if ((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')) putc(ch, fc); else if (ch>='0'&&ch<='9') putc(ch, fd); else putc(ch, fs); } fclose(fp1); fclose(fv); fclose(fc); fclose(fd); fclose(fs); return 0; }

7. Store & Read Integers from File

#include <stdio.h> int main() { FILE *fp; int number; fp = fopen("Master.dat", "w"); printf("Enter integers (Ctrl+Z to stop):\n"); while (scanf("%d", &number) != EOF) putw(number, fp); fclose(fp); fp = fopen("Master.dat", "r"); printf("File contents:\n"); while ((number = getw(fp)) != EOF) printf("%d\n", number); fclose(fp); return 0; }

8. Separate Even & Odd Numbers

#include <stdio.h> int main() { FILE *fp, *fe, *fo; int num; fp = fopen("Master.dat", "w"); printf("Enter numbers:\n"); while (scanf("%d", &num) != EOF) putw(num, fp); fclose(fp); fp = fopen("Master.dat", "r"); fe = fopen("even.dat", "w"); fo = fopen("odd.dat", "w"); while ((num = getw(fp)) != EOF) { if (num % 2 == 0) putw(num, fe); else putw(num, fo); } fclose(fp); fclose(fe); fclose(fo); return 0; }

9. Store & Read String from File

#include <stdio.h> int main() { char str[30]; FILE *fp; fp = fopen("test2.txt", "w"); printf("Enter text:\n"); while (gets(str) != NULL) { fputs(str, fp); fputs("\n", fp); } fclose(fp); fp = fopen("test2.txt", "r"); while (fgets(str, 30, fp) != NULL) puts(str); fclose(fp); return 0; }

10. Count Number of Lines in File

#include <stdio.h> int main() { FILE *fp; char ch; int count = 0; fp = fopen("test2.txt", "r"); while ((ch = fgetc(fp)) != EOF) { if (ch == '\n') count++; } printf("Number of lines: %d", count); fclose(fp); return 0; }

Comments