PlusTwo

P.S.E.B Syllabus (Computer Application)

              String Function using C Programming Language


you'll learn to manipulate strings in C using library functions such as gets(), puts, strlen() and more. You'll learn to get string from the user and perform operations on the string. You need to often manipulate strings according to the need of a problem. Most, if not all, of the time string manipulation can be done manually but, this makes programming complex and large.


To solve this, C supports a large number of string handling functions in the standard library"string.h". Few commonly used string handling functions are discussed below:

FunctionWork of Function
strlen()Calculates the length of string
strcpy()Copies a string to another string
strcat()Concatenates(joins) two strings
strcmp()Compares two string
strlwr()Converts string to lowercase
strupr()Converts string to uppercase
Strings handling functions are defined under "string.h" header file.
#include <string.h>
Note: You have to include the code below to run string handling functions.

gets() and puts()

Functions gets() and puts() are two string functions to take string input from the user and display it respectively as mentioned in the previous chapter.
#include<stdio.h>

int main()
{
    char name[30];
    printf("Enter name: ");
    gets(name);     //Function to read string from user.
    printf("Name: ");
    puts(name);    //Function to display string.
    return 0;
}
Note: Though, gets() and puts() function handle strings, both these functions are defined in "stdio.h" header file.

C strlen()

The strlen() function calculates the length of a given string.

C strlen() Prototype

size_t strlen(const char *str);
The function takes a single argument, i.e, the string variable whose length is to be found, and returns the length of the string passed.
The strlen() function is defined in <string.h> header file.
calculation of string using strlen() library function.

Example: C strlen() function

#include <stdio.h>
#include <string.h>
int main()
{
    char a[20]="Program";
    char b[20]={'P','r','o','g','r','a','m','\0'};
    char c[20];

    printf("Enter string: ");
    gets(c);

    printf("Length of string a = %d \n",strlen(a));

    //calculates the length of string before null charcter.
    printf("Length of string b = %d \n",strlen(b));
    printf("Length of string c = %d \n",strlen(c));

    return 0;
}

C strcpy()

The strcpy() function copies the string to the another character array.

strcpy() Function prototype

char* strcpy(char* destination, const char* source);
The strcpy() function copies the string pointed by source (including the null character) to the character array destination.
This function returns character array destination.

The strcpy() function is defined in string.h header file.

Example: C strcpy()

#include <stdio.h>
#include <string.h>
int main()
{
char str1[10]= "awesome";
char str2[10];
char str3[10];
strcpy(str2, str1);
strcpy(str3, "well");
puts(str2);
puts(str3);
return 0;
}

C strcat()

The function strcat() concatenates two strings.
In C programming, strcat() concatenates (joins) two strings.
The strcat() function is defined in <string.h> header file.

C strcat() Prototype

char *strcat(char *dest, const char *src)
It takes two arguments, i.e, two strings or character arrays, and stores the resultant concatenated string in the first string specified in the argument.
The pointer to the resultant string is passed as a return value.

Example: C strcat() function

#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "This is ", str2[] = "programiz.com";
//concatenates str1 and str2 and resultant string is stored in str1.
strcat(str1,str2);
puts(str1);
puts(str2);
return 0;
}

C strcmp()

The strcmp() function compares two strings and returns 0 if both strings are identical.

C strcmp() Prototype

int strcmp (const char* str1, const char* str2);
The strcmp() function takes two strings and return an integer.

The strcmp() compares two strings character by character. If the first character of two strings are equal, next character of two strings are compared. This continues until the corresponding characters of two strings are different or a null character '\0' is reached.
It is defined in string.h header file.

Return Value from strcmp()

Return ValueRemarks
0if both strings are identical (equal)
negativeif the ASCII value of first unmatched character is less than second.
positive integerif the ASCII value of first unmatched character is greater than second.

Example: C strcmp() function

#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;
// comparing strings str1 and str2
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);
// comparing strings str1 and str3
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result);
return 0;
}

C – strupr() function

  • strupr( ) function converts a given string into uppercase. Syntax for strupr( ) function is given below.
    char *strupr(char *string);
  • strupr( ) function is non standard function which may not available in standard library in C.

EXAMPLE PROGRAM FOR STRUPR() FUNCTION IN C:

In this program, string “Modify This String To Upper” is converted into uppercase using strupr( ) function and result is displayed as “MODIFY THIS STRING TO UPPER”.

C – strupr() function

  • strupr( ) function converts a given string into uppercase. Syntax for strupr( ) function is given below.
    char *strupr(char *string);
  • strupr( ) function is non standard function which may not available in standard library in C.

EXAMPLE PROGRAM FOR STRUPR() FUNCTION IN C:

In this program, string “Modify This String To Upper” is converted into uppercase using strupr( ) function and result is displayed as “MODIFY THIS STRING TO UPPER”.

C – strrev() function

  • strrev( ) function reverses a given string in C language. Syntax for strrev( ) function is given below.char *strrev(char *string);
  • strrev( ) function is non standard function which may not available in standard library in C.

EXAMPLE PROGRAM FOR STRREV() FUNCTION IN C:

In below program, string “Hello” is reversed using strrev( ) function and output is displayed as “olleH”.

 

#include<stdio.h>
#include<string.h>
void concat(char[], char[]);
int main() {
 char s1[50], s2[30];
 printf("\nEnter String 1 :");
 gets(s1);
 printf("\nEnter String 2 :");
 gets(s2);
 concat(s1, s2);
 printf("\nConcated string is :%s", s1);
 return (0);
}
void concat(char s1[], char s2[]) {
 int i, j;
 i = strlen(s1);
 for (j = 0; s2[j] != '\0'; i++, j++) {
  s1[i] = s2[j];
 }
 s1[i] = '\0';
}

Output

The above code sample will produce the following result.
Enter String 1 : Tejinder
Enter String 2 : Singh
Concated string is : TejinderSingh

Explanation of Program

Step 1. input

let two string variable s1 and s2 having input
s1 = “Tejinder”;
s2 = “Singh”;

Step 2. Found Last position

Then found length of first string
= strlen(s1);
= strlen(“Tejinder”);
= 5
Last position of s1 = 5

STEP 3. Concatenat

Now we can Concatenate second string using for loop
s1[5] = “S”
s1[6] = “i”
s1[7] = “n”
s1[8] = “g”
s1[9] = “h”

#include<stdio.h>
 int main() {
   char str1[30], str2[30];
   int i;
   printf("\nEnter two strings :");
   gets(str1);
   gets(str2);
   i = 0;
   while (str1[i] == str2[i] && str1[i] != '\0')
      i++;
   if (str1[i] > str2[i])
      printf("str1 > str2");
   else if (str1[i] < str2[i])
      printf("str1 < str2");
   else
      printf("str1 = str2");
   return (0);
}


Output :

Explanation Of Program :

Firstly find the length of the string using library function strlen().
Suppose we accepted String “Pritesh” then –
As we know String is charracter array and Character array have character range between 0 to length-1. Thus we have position of last character in variable ‘j’.Current Values of ‘i’ and ‘j’ are –
‘i’ positioned on first character and ‘j’ positioned on last character. Now we are swapping characters at position ‘i’ and ‘j’. After interchanging characters we are incrementing value of ‘i’ and decrementing value of ‘j’.
If i crosses j then process of interchanging character is stopped. 
                                                                                                                                                                        NEXT>>

Comments

Popular posts from this blog

View of Punjab School