String and pointer in C Programming

String in C Programming

  • A string is a group of characters.

  • In C, strings are stored as a character array (e.g., char ch[100];).

  • Each character occupies an index in the array. For example, the first character is at index 0, the second at index 1, and so on.

  • The value of a string is essentially a group of characters.

  • Syntax of string in C:
    char ch[100];

  • It’s clear from the above that a user can store a string if the memory is available.

Accepting and Displaying a String

  • Example 1: A C program to accept and display a string from the user console.

    #include <stdio.h>
    #include <conio.h>
    
    void main() {
        char str1[100];
        clrscr();
        printf("\n Enter any string: ");
        scanf("%s", str1);
        printf("\n Your entered string = %s", str1);
        getch();
    }
    
  • %s is used in scanf to read the input string.

Using gets() Function

  • Example 2: Using gets() to accept a string.

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    void main() {
        char str1[100];
        clrscr();
        printf("\n Enter any string: ");
        gets(str1);
        printf("\n Your entered String = %s", str1);
        getch();
    }
    
  • gets() is a predefined method in C used to accept a string from the user.

  • It stores the accepted string in the specified array location.

  • Syntax: gets(variable_name); (e.g., gets(str1);)

  • gets() is an alternative to scanf(). It accepts an entire line of input, including spaces, until a newline character is encountered.

Alternate example of displaying a string

  • Example 3: Another way to display a string
    c void main() { char str1[100]; clrscr(); printf("\n Enter any string: "); flushall(); gets(str1); printf("\n Your entered string = %s", str1); getch(); }

Finding the Length of a String

  • Example 4: A C program to accept a string and display its length without using the standard strlen() method.

    #include <stdio.h>
    #include <conio.h>
    
    void main() {
        int i = 0;
        int len = 0;
        char str[100];
        clrscr();
        printf("\n Enter the string: ");
        flushall();
        gets(str);
        while (str[i] != '\0') {
            len++;
            i++;
        }
        printf("\n The length of the string = %d", len);
        getch();
    }
    
  • The loop continues until the null character \0 is encountered.

  • Example 5: Finding the length of a string using the standard strlen() method.

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    void main() {
        int len = 0;
        char str[100];
        clrscr();
        printf("\n Enter any string: ");
        scanf("%s", str);
        len = strlen(str);
        printf("\n Length of the String = %d", len);
        getch();
    }
    
  • strlen() is a standard C function used to find the length of a string.

  • Syntax: int x = strlen(string);

Copying a String

  • Example 6: A C program to accept a string from the user and copy it into another array without using the standard method.

    #include <stdio.h>
    #include <conio.h>
    
    void main() {
        char str1[100];
        char str2[100];
        int i = 0;
        clrscr();
        printf("\n Enter any string: ");
        scanf("%s", str1);
        while (str1[i] != '\0') {
            str2[i] = str1[i];
            i++;
        }
        str2[i] = '\0';
        printf("\n Copied String = %s", str2);
        getch();
    }
    
  • Example 7: A C program to copy a string into another array using the standard method strcpy().

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    void main() {
        char str1[100];
        char str2[100];
        clrscr();
        printf("\n Enter any string: ");
        scanf("%s", &str1);
        strcpy(str2, str1);
        printf("\n Copied string = %s", str2);
        getch();
    }
    
  • strcpy(destination, source) copies the string from source to destination.

String Concatenation

  • Example 8: A C program to accept two strings from the user and copy them into a third character array.

    #include <stdio.h>
    #include <conio.h>
    
    void main() {
        char str1[100];
        char str2[100];
        char str3[100];
        int i = 0, j = 0, k = 0;
        clrscr();
        printf("\n Enter first string: ");
        scanf("%s", &str1);
        printf("\n Enter second string: ");
        scanf("%s", &str2);
        while (str1[i] != '\0') {
            str3[k] = str1[i];
            i++;
            k++;
        }
        while (str2[j] != '\0') {
            str3[k] = str2[j];
            j++;
            k++;
        }
        str3[k] = '\0';
        printf("\n Merged String = %s", str3);
        getch();
    }
    
  • Example 9: A C program to concatenate the second string at the end of the first string using the standard method strcat().

    #include <stdio.h>
    #include <conio.h>
    
    void main() {
        char str1[100];
        char str2[100];
        clrscr();
        printf("\n Enter the first string: ");
        scanf("%s", &str1);
        printf("\n Enter the second string: ");
        scanf("%s", &str2);
        strcat(str1, str2);
        printf("\n Concatenated String = %s", str1);
        getch();
    }
    
  • strcat(destination, source) concatenates the string source to the end of the destination string.

  • Example 10: A C program to copy the second string at the end of the first string using both strcpy() and strcat().

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    void main() {
        char str1[100];
        char str2[100];
        char str3[100];
        clrscr();
        printf("\n Enter the first string: ");
        scanf("%s", &str1);
        printf("\n Enter the second string: ");
        scanf("%s", &str2);
        strcpy(str3, str1);
        strcat(str3, str2);
        printf("\n Merged string = %s", str3);
        getch();
    }
    

String Reversal

  • Example 12: A C program to accept a string from the user and display the reverse of that string using the standard method strrev().

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    void main() {
        char str1[100];
        clrscr();
        printf("\n Enter the 1st string: ");
        scanf("%s", &str1);
        strrev(str1);
        printf("\n Reverse of String = %s", str1);
        getch();
    }
    
  • Example 13: String reversal without using standard method.

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    void main() {
        char str1[100];
        int i = 0;
        int len = 0;
        clrscr();
        printf("\n Enter the 1st string: ");
        scanf("%s", &str1);len = strlen(str1);
    printf("\n Reverse of the String is ");
    
    for (i = len - 1; i &gt;= 0; i--)
        printf("%c", str1[i]);
    
    getch();
    }
    

String Comparison

  • strcmp() is a standard method in C used to compare two strings.

  • If the two strings are equal, it returns 0.

  • If the first string is greater than the second string, it will return a positive value.

  • If the first string is less than the second string, it will return a negative value.

  • Syntax: strcmp(string1, string2);

String Comparison Examples

  • Example 14: A C program to accept two strings from the user and check whether the strings are equal or not.

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    void main() {
        char str1[100], str2[100];
        clrscr();
        printf("\n Enter first string: ");
        scanf("%s", str1);
        printf("\n Enter second string: ");
        scanf("%s", str2);
        if (strcmp(str1, str2) == 0)
            printf("\n Two Strings are equal");
        else
            printf("\n Two strings are not equal");
        getch();
    }
    
  • Example 15: A C program to accept two strings from the user and display the greater string.

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    void main() {
        char str1[100], str2[100];
        clrscr();
        printf("\n Enter first string: ");
        scanf("%s", str1);
        printf("\n Enter second string: ");
        scanf("%s", str2);if (strcmp(str1, str2) &gt; 0)
        printf("\n First string is greater");
    else
        printf("\n Second string is greater");
    getch();
    }
    

Considering Space in String Input

  • To consider spaces in string input, the following methods can be used in C programming.

    1. gets()

    2. fgets(): This is also a standard method in C programming that is used to consider space in string format.

      • Syntax: fgets(str1, 100, stdin);

    3. scanf(): This is a standard method available in stdio.h and is used to read all characters in a string, including spaces.

      • Syntax: scanf("%[^\n]%*c", str1);

String and Functions

  • Example 17: A C program to accept a string from the user and display that string using a function.

    #include <stdio.h>
    #include <conio.h>
    
    void display(char[]);
    
    void main() {
        char str1[100];
        clrscr();
        printf("\n Enter any string: ");
        scanf("%s", &str1);
        display(str1);
        getch();
    }
    
    void display(char str1[100]) {
        printf("\n String from main function = %s", str1);
    }
    
  • Example 18: Copy string into another string using function

    #include <stdio.h>
    #include <conio.h>
    void display(char[]);
    void main()
    {
        char str1[100];
        char str2[100];
        int i = 0;
        clrscr();
        printf("\n Enter the string: ");
        scanf("%s", str1);
        display(str1);
        getch();
    }
    
    void display(char str1[100])
    {
        char str2[100];
        int i = 0;
        while (str1[i] != '\0')
        {
            str2[i] = str1[i];
            i++;
        }
        str2[i] = '\0';
        printf("\n Copied string = %s", str2);getch();
    }
    
  • Example 19:

    #include <stdio.h>
    #include <conio.h>
    void main()
    {
        char str1[100];
        char str2[100];
        int ch = 0;
        int i = 0;
        int len = 0;
        clrscr();
        printf("\n Enter any string: ");
        scanf("%s", str1);printf("\n 1. Display length of the string\n ");
    printf("\n 2. Copy one string to another char string\n ");
    printf("\n Enter your choice: ");
    scanf("%d", &amp;ch);
    switch (ch)
    {
    case 1:
    {
        while (str1[i] != '\0')
        {
            len++;
            i++;
        }
        printf("\n The length of the string = %d", len);
    }
    break;
    case 2:
    {
        while (str1[i] != '\0')
        {
            str2[i] = str1[i];
            i++;
        }
        str2[i] = '\0';
        printf("\n Copied string = %s", str2);
    }
    break;
    }
    getch();
    }
    

Pointers in C

  • A pointer is a variable that holds the memory address of another variable.

  • Pointers are represented with the * symbol.

  • When variables are stored in memory, they have specific addresses.

  • Using a pointer, you can access and manipulate the data stored at that memory address.

  • The address of operator & is used to get the address of a variable.

  • Dereferencing a pointer (using *) allows you to access the value stored at the pointer's address.

  • This is also known as "call by reference”.

  • Sending the address of a variable to a function allows the function to modify the original variable.

Types of Pointers:
  • Integer Pointer: Points to an integer variable.

  • Array Pointer: Points to an array.

  • Structure Pointer: Points to a structure.

  • Function Pointer: Points to a function.

  • Null Pointer: A pointer that does not point to any valid memory location and is often represented as (void *)0.

  • Void Pointer: A pointer that can point to any type of variable. The syntax is (void *).

  • Near Pointer: Can only point to memory locations within the current segment.

  • Far Pointer: Can point to memory locations outside the current segment.

  • Constant Pointer: The value of the pointer does not change even if the memory location of pointer is changed. (using const outside)

Issues with Pointers
  • Wild Pointer: A pointer that has not been initialized and points to an unknown memory location. Using a wild pointer can cause the program to crash or behave unpredictably.

  • Dangling Pointer: A pointer that points to a memory location that has already been freed or deallocated.

  • Hanging Pointer: A pointer that points to a memory location that is no longer being used, but the memory is still occupied, and calls dangling pointer.

Pointer Example

  • #20: Using Pointer

    //Write a C program to accept any number & display that no using pointer
    #include <stdio.h>
    #include <conio.h>
    void display(int *);
    void main()
    {
        int a = 0;
        clrscr();
        printf("\n Enter any number: ");scanf("%d", &amp;a);
    display(&amp;a);
    
    getch();
    }
    void display(int *x)
    {
        printf("\n The no from main function = %d", *x);
    }
    

Call by reference

  • #21: call by reference

    // Write a C program to accept two no from user & display their addition by call by reference(Pointer)
    #include <stdio.h>
    #include <conio.h>
    void add(int *, int *);
    void main()
    {
        int a = 0;
        int b = 0;
        clrscr();
        printf("\n Enter the 1st number: ");
        scanf("%d", &a);
        printf("\n Enter the 2nd number: ");
        scanf("%d", &b);
        add(&a, &b);
        getch();
    }
    void add(int *x, int *y)
    {
        int ans = *x + *y;printf("\n The Addition = %d", ans);
    }
    

Arithmetic Operations.

  • #22: addition using pointer

    //Write a program to accept two no from uses & display their add " using pointes :-
    #include <stdio.h>
    #include <conio.h>
    void main()
    {
        int a = 0;
        int b = 0;
        int *a1 = 0;
        int *a2 = 0;
        int ans = 0;
        clrscr();
        printf("\n Enter the 1st number: ");
        scanf("%d", &a);
        printf("\n Enter the 2nd number: ");
        scanf("%d", &b);
        a1 = &a;
        a2 = &b;
        ans = (*a1) + (*a2);
        printf("\n Addition: %d", ans);
        getch();
    };
    
  • #23: subtraction using pointer

    //Subtraction using  Pointer
    #include <stdio.h>
    #include <conio.h>
    void main()
    {
        int a = 0;
        int b = 0;
        int *a1 = 0;
        int *b1 = 0;
        clrscr();
        printf("\n Enter 1st za (no: ");
        scanf("%d", &a);
        printf("\n Enter 2nd za nu: ");
        scanf("%d", &b);
        a1 = &a;
        b1 = &b;
        int ans = (*a1) - (*b1);
        printf("\n Subtraction = %d", ans);
        getch();
    }
    

Swapping Numbers

  • #24: Swapping of two no.

    #include <stdio.h>
    #include <conio.h>
    void main()
    {
        int a = 0;
        int b = 0;
        int temp = 0;
        clrscr();
        printf("\n Enter 1st nu : ");
        scanf("%d", &a);
        printf("\n Enter 2st nu: ");
        scanf("%d", &b);
        printf("\n The values before, scorffing. \n");
        printf("\n first no =%d", a);
        printf("\n Secad ra =%d", b);
        int *a1 = &a;
        int *b1 = &b;
        temp = *a1;
        *a1 = *b1;
        *b1 = temp;printf("\n The values after swraffing la \n ");
    printf("\n First za = %d", a);
    printf("\n Secad no =%d", b);
    
    getch();
    };
    

Reverse order & Even/Odd

  • #25: Reverse
    ```c
    #include
    #include
    //Write a c program to accept one number from uses
    //& display the reverse of that nes by using call by reference.
    void reverse(int *);
    void main()
    {
    clrscr();
    int a;
    printf("\n Enter any number: ");
    scanf("%d", &a);
    reverse(&a);
    getch();
    }
    void reverse(int *n)
    {
    int rem = 0;
    int rev = 0;
    while ((n) != 0) { rem = (n) % 10;
    rev = (rev * 10) + rem;
    (n) = (n) / 10;
    }
    printf("\n Reverse of ther not = %d ", rev);
    };

*   **#26**: Even/Odd using pointer

    ```c

    //Write wheather no. is even & odd using pointer
    #include <stdio.h>
    #include <conio.h>
    void main()
    {
        int z = 0;
        int *a = 0;
        clrscr();
        printf("\n Entre any number: ");
        scanf("%d", &z);
        a = &z;
        if ((*a % 2) == 0)
        {
            printf("\n This z= is even\n ");
        }
        else
        {
            printf("\n This z= is odd ");
        }
        getch();
    };
    ```

## More Pointer examples

*   **#27**:  Write code to display the sum of the individual digits of input numbers.

c
//Pas Accept any number from uses
//add & disfley that digits of the number
// from the console & display the add
#include
#include
// #include
void digitadd(int *);
void main()
{
clrscr();
int a = 0;
printf("\n Enter any number: ");
scanf("%d", &a);
digitadd(&a);
getch();
}

void digitadd(int *no)
{
    int rem = 0;
    int sum = 0;
    while ((*no) != 0)
    {
        rem = (*no) % 10;
        sum = sum + rem;

        *no = (*no) / 10;
    }

    printf("\n Add of the digits = %d\n