Computer Programming C: UNIT II: Arrays and Strings

Sample C programs on Strings

Computer Programming C: Arrays and Strings - C Programming - Sample C programs on Strings

CASE STUDIES

(Sample programs on Strings)

 

1. Write a program to concatenate two strings.

String concatenation is the process of combining two strings into one. Here the user must supply two strings from the input. The concatenated string can be obtained by adding each character from the first string into new string after finishing this then add second string each character into new string and finally place the null character '\0' at the end of the new strings. This process can be easily obtained by using the string function strcat().

#include <stdio.h>

#include <conio.h>

void main()

{

      char str1[10], str2[10], str3[40];

      int i,j;

      clrscr();

      printf("Enter the two string : ");

      scanf("%s %s", strl, str2);

      for(i=0;str1[i]!= '\0';i++)

      {

            str3[i]= strl[i];

      }

      for(j=0;str2[j]!= '\0';j++)

      {

            str3[i]= str2[j];

            i++;

      }

      str3[i]='\0';

      printf("Concatenated string is...... %s\n", str3);

      getch();

}

OUTPUT 1

Enter the two string : venkat muni

Concatenated string is...... venkatmuni

OUTPUT 2:

Enter the two string: VRB Publishers

Concatenated string is...... VRBPublishers

 

2. Write a program to compare two strings.

String comparison: String comparison is the process of comparing two string for their equality. Here, the user has to supply two strings to compare. Here each array character from one string can be checked for equality with another string until both string array finished. If both are equal, then print equal or print not equal.

#include <stdio.h>

#include <conio.h>

void main()

{

int i;

char str1[20], str2[20];

printf("Enter the first and second string : ");

scanf("%s %s", strl, str2);

for(i=1;i<2;i++)

{

      if(str][i] == str2[i])

            printf("The both string is equal ");

      else

            printf("The both string is not equal");

getch();

}

}

OUTPUT 1

Enter the first and second string : venkat venkat

The both string is equal

OUTPUT 2

Enter the first and selcond string: suresh ramesh

The both string is not equal

 

3. Write a program to find out string length.

Length of the string is the process of counting the number of characters presented in the string arrays. Here, the user has to supply one string to count their characters or lengths. Set up the for loop until null string occurrence as well as increment the low variable to get the length of the string.

#include <stdio.h>

#include <conio.h>

void main()

{

char str[50];

int len;

printf("Enter the string: ");

scanf("%[\n]", str);

for(len = 0;str[len]!= '\0';len++);

       printf("Length of main string is %d\n",len);

getch();

}

OUTPUT

Enter the string: VRB Publishers, Vadapalani, Chennai‒26.

Length of main string is 39

 

4. Write a program to copy the string.

String copying is the process of copying one string into another, the another may be new string arrays or arrays string. Here copy each character from one string into other strings until null character occurrence and finally place null character at the end of destination string.

#include <stdio.h>

#include <conio.h>

void main()

{

int i;

char str1 [50], str2 [50];

printf("\nEnter the first string : ");

scanf("%s", strl);

printf("The first string is %s", strl);

for(i=0;str1[i]!= '\0';i++)

{

        str2[i]=str1[i];

}

str2[i]='\0';

printf("\n copy the string 1 string to 2 without using strcpy 0 function");

printf("\n The copied string is %s", str2);

}

OUTPUT

Enter the first string : MUNISUJI

The first string is MUNISUJI

copy the string 1 to string 2 without using strcpy() function

The copied string is MUNISUJI.

 

5. Write a program to arrange names in Alphabetical order

Arranging names in alphabetical order is nothing but the string sorting means that the names are arranged in either ascending or descending order by comparing each of the name.

Here, first of all, we have to capture the names into an array up to the keyword END encounters from the keyboard. Then we can compare the first string with subsequent strings using selection sort method if it gives greater than zero comparison result then move them to appropriate place using another variable until all strings are compared.

#include <stdio.h>

#include <conio.h>

main()

{

char names[20][20], temp[10], c;

int i,j,k,n=0;

printf("Enter Names to stop type keyword END ………..\n");

scanf("%s", names[n]);

while(strcmp(names[n], "END") > 0)

{

         n++;

         scanf("%s", names[n]);

}

printf("\n");

for(i=0;i<n;i++)

printf("%10s", names[i]);

printf("\n");

printf("\n");

printf("Number of Names....................  %d\n", n);

for(i=0;i<n‒1;i++)

{

     for(j=i+1;j<n;j++)

    {

        if (strcmp(names[i], names[j])>0)

        {

            strcpy(temp, names[i]);

            strcpy (names[i], names[j]);

            strcpy(names[j], temp);

        }

    }

printf("After Sorting the Names are.....\n");

for(i=0;i<n;i++)

printf("%10s", names[i]);

printf("\n");

}

OUTPUT

Enter Names to stop type keyword END……..

 suji lak muni anil durga anil chiru babu END

suji lak muni anil durga anil chiru babu

Number of Names ………. 8

After Sorting the Names are …..

anil anil babu chiru durga lak muni suji

 

Computer Programming C: UNIT II: Arrays and Strings : Tag: Computer Science : - Sample C programs on Strings


Computer Programming C: UNIT II: Arrays and Strings



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation