Tuesday, August 16, 2011

String operations (length, compare, copy, concatenate) without including string.h using C

In C, string.h includes various build in functions for string operations. The main operations are
1. Length of the string (strlen)
The syntax of strlen is :
strlen(string);
It calculates the length of the string and returns its length. For example:

#include<string.h>
string = "Mumbai";
printf("Length = %d",strlen(string));

The above code displays 5 because Mumbai consists of 5 characters. Note: it does not count null character.

2. Joining two strings (strcat)

The syntax of strcat is
strcat(string1,string2);
Now it removes the null character from string1 and joins the first character of string2 at that position. Now, string1 consists of both string1 and string2 in joined form. Example:

#include<string.h>=
char string1[] = "Anti";
char string2[] = "Particle";
strcat(string1,string2);=
printf("%s",string1); //display AntiParticle

3. Comparing two strings(strcmp)

The syntax of strcmp is
strcmp(string1,string2);
It returns 0 if string1 is same as string2 and returns 1 if they are not same. Example:

#include<string.h>
char string1 = "Nepal";
char string2 = "Srilanka";
if(strcmp(string1,string2)==0){
printf("They are equal");
}else{
printf("They are not equal"); //this is executed
} 

4. Copying one string to another (strcpy)

The syntax of strcpy is

strcpy(destination_string, source_string);

It copies the content of source_string to destination_string. Example:

#include<string.h>
char source[] = "Hello";
char destination[10]; //uninitialized
strcpy(destination,source);
printf("%s",destination); //prints Hello 
These are some of the functions in string.h for string operation. To use these functions you must include header file <string.h>. But we can make our own functions to perform above task without including string,h. Here is the complete source code that has own functions find_length (like strlen) to find the length, join_strings( like strcat) for joining strings, compare_strings(like strcmp) for comparing two strings and copy_string(like strcpy) to copy one string from another. Observer carefully the code, if you are a beginner, you will learn a lot of things about string operation.

Source Code


///fundamental string operation, lenth, concatenation, compare and copy strings without string.h
#
include < stdio.h > #include < stdlib.h >
  int find_length(char string[]) {
    int len = 0, i;
    for (i = 0; string[i] != '\0'; i++) {
      len++;
    }
    return len;
  }
void join_strings(char string1[], char string2[]) {
    int i, len1, len2;
    len1 = find_length(string1);
    len2 = find_length(string2);
    for (i = len1; i < len1 + len2; i++) {
      string1[i] = string2[i - len1];
    }
    string1[i] = '\0'; //adding null character at the end of input
  }
  /*returns 0 if thery are same otherwise returns 1*/
int compare_strings(char string1[], char string2[]) {
  int len1, len2, i, count = 0;
  len1 = find_length(string1);
  len2 = find_length(string2);
  if (len1 != len2)
    return 1;
  for (i = 0; i < len1; i++) {
    if (string1[i] == string2[i])
      count++;
  }
  if (count == len1)
    return 0;
  return 1;
}
void copy_string(char destination[], char source[]) {
  int len, i;
  len = find_length(source);
  for (i = 0; i < len; i++) {
    destination[i] = source[i];
  }
  destination[i] = '\0';
}
int main() {
  char string1[20], string2[20]; //string variables declaration with size 20
  int choice;
  while (1) {
    printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n");
    printf("Enter your choice: ");
    scanf("%d", & choice);
    switch (choice) {
    case 1:
      printf("Enter the string: ");
      scanf("%s", string1);
      printf("The length of string is %d", find_length(string1));
      break;
    case 2:
      printf("Enter two strings: ");
      scanf("%s%s", string1, string2);
      join_strings(string1, string2);
      printf("The concatenated string is %s", string1);
      break;
    case 3:
      printf("Enter two strings: ");
      scanf("%s%s", string1, string2);
      if (compare_strings(string1, string2) == 0) {
        printf("They are equal");
      } else {
        printf("They are not equal");
      }
      break;
    case 4:
      printf("Enter a string: ");
      scanf("%s", string1);
      printf("String1 = %s\n");
      printf("After copying string1 to string 2\n");
      copy_string(string2, string1);
      printf("String2 = %s", string2);
      break;
    case 5:
      exit(0);
    }
  }
  return 0;
}

Output


string

3 comments:

  1. Please can any1 tell me menu driven program for string operations like copy,compare and concat two strings using pointers and functions

    ReplyDelete
  2. ALL CAN CHECK IT

    #include

    #include

    int find_length(char string[]){

    int len = 0,i;

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

    len++;

    }

    return len;

    }

    void join_strings(char string1[], char string2[]){

    int i, len1, len2;

    len1 = find_length(string1);

    len2 = find_length(string2);

    for(i = len1; i < len1+len2; i++){

    string1[i] = string2[i-len1];

    }

    string1[i] = '\0'; //adding null character at the end of input

    }

    /*returns 0 if thery are same otherwise returns 1*/

    int compare_strings(char string1[], char string2[]){

    int len1, len2, i, count = 0;

    len1 = find_length(string1);

    len2 = find_length(string2);

    if(len1!=len2)

    return 1;

    for(i = 0; i < len1; i++){

    if(string1[i] == string2[i])

    count++;

    }

    if(count == len1)

    return 0;

    return 1;

    }

    void copy_string(char destination[], char source[]){

    int len,i;

    len = find_length(source);

    for(i = 0; i < len; i++){

    destination[i] = source[i];

    }

    destination[i] = '\0';

    }

    int main(){

    char string1[20], string2[20]; //string variables declaration with size 20

    int choice;

    while(1){

    printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n");

    printf("Enter your choice: ");

    scanf("%d",&choice);

    switch(choice){

    case 1:

    printf("Enter the string: ");

    scanf("%s",string1);

    printf("The length of string is %d", find_length(string1));

    break;

    case 2:

    printf("Enter two strings: ");

    scanf("%s%s",string1,string2);

    join_strings(string1,string2);

    printf("The concatenated string is %s", string1);

    break;

    case 3:

    printf("Enter two strings: ");

    scanf("%s%s",string1,string2);

    if(compare_strings(string1,string2)==0){

    printf("They are equal");

    }else{

    printf("They are not equal");

    }

    break;

    case 4:

    printf("Enter a string: ");

    scanf("%s",string1);

    printf("String1 = %s\n");

    printf("After copying string1 to string 2\n");

    copy_string(string2,string1);

    printf("String2 = %s",string2);

    break;

    case 5:

    exit(0);

    }

    }

    return 0;

    }

    ReplyDelete