String Manipulating Functions

This note is about the String Manipulating Functions. It explain the different string function they are; strlen() function, strrev() function, strlwr() and strupr() function, strcpy() function, stract() function and strcmp() function. These functions are used for string handling.

Summary

This note is about the String Manipulating Functions. It explain the different string function they are; strlen() function, strrev() function, strlwr() and strupr() function, strcpy() function, stract() function and strcmp() function. These functions are used for string handling.

Things to Remember

  1. The handling of string is totally different from handling the numbers. Some of the important functions used to manipulate strings are descried with the help of examples in preceding sections.
  2. The strlen() function is used to measure the length of the string accepted by the user or declared in the array of string.
  3. The strrev () function is used to reverse the given string. Hence all the characters given by the user are reversed.
  4. The strlwr() and strupr() functions converts the uppercase characters of the given string into lowercase whereas the strupr() function converts lowercase characters into uppercase.
  5. The strcat() function is generally used to merge two strings and produce single string. Hence, both the strings are concatenated and a new string is in the form of merged string.
  6. The strcpy() function is used to copy a string from one variable into another variable where both the variables are of same type.
  7. The strcmp() function compares two strings and produces the result to store in a numeric variable. If both the strings are like, it produces 0 whereas 1 is produced when both strings are not alike. 

MCQs

No MCQs found.

Subjective Questions

No subjective questions found.

Videos

No videos found.

String Manipulating Functions

String Manipulating Functions

Generally, the early programmers think that the strings are handled in the similar fashion as numbers in C programming. The handling of strings is totally different from handling the numbers. Some of the important functions used to manipulate strings are described with the help of examples in preceding sections.

  • The strlen () function

This function is used to measure the length of the string accepted by the user or declared in the array of string.

Syntax : strlen(string)

# Write a program to reverse a string and measure its length also.

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main ()

{

int st1;

char n[]=”Kathmandu university”;

clrscr ();

printf(“\n Example of strlen function\n”);

stl=strlen (n);

printf(“\n Reversed string:\n”);

for (int i=st1; i>=0; i--);

}

printf(“\n String is: %s”, n);

printf(“\n Length = %d”, st1);

}

  • The strrev () function

This function is used to reverse the given string. Hence all the characters given by the user are reversed.

Syntax: streev(string);

# Write a program to demonstrate the use of strrev () function.

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main ()

{

int st1;

char n[] =”Kathmandu university”;

clrscr (0;

printf(“\n Example of strrev function”);

printf(“\n Before reversing: %s”, n);

strrev (n);

printf(“\n After reversing : %s”, n);

}

  • The strlwr() and strupr() functions

The strlwr() function converts the uppercase characters of the given string into lowercase whereas the strupr() function converts lowercase characters into uppercase.

Syntax: strlwr(string); strupr(string);

# Write a program to test the strlwr() and strupr() functions.

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main ()

{

int st1;

char string[] = “Kathmandu University”);

clrscr ();

printf(“\n Example of upper and lower functions”);

printf(“\n Original string is: %s”, string);

printf(“\n Lowercase of the string is: %s”, strlwr (string));

printf(“\n Uppercase of the string is: %s”, strupr(string));

}

  • The strcpy () function

The strcpy() function is used to copy a string from one variable into another variable where both the variables are of the same type. You cannot copy data stored in the numeric variable into a string variable.

Syntax: strcpy (target,source);

Demonstrate strcpy() function

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main ()

{

int st1;

char soruce[]=”Kathmandu University”;

char target[50];

clrscr ();

printf(“\n Example of strcpy () function”);

strcpy (target, source );

printf(“\n Original source string is: %s”, source);

printf(“\n Target string is: %s”, target);

}

In the above program, the content of source variable is passed to target variable. If we print both the variables, we get the same result.

  • The strcat() function

This function is generally used to merge two strings and produce the single string. Hence, both the strings are concatenated and a new string is in the form of merged string.

Syntax: stract(target, source);

# Write a program to right concatenate two strings “International” and “Forum”.

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main ()

{

int st1;

char source[] =”Forum”;

char target[] =”International”;

clrscr ();

printf(“\n First string: %s”, source);

printf(“\n Second string: %s”, target);

printf(“\n After concatenating the strings:”);

strcat(target, source);

printf(“\n Example of strcat () function”);

printf(“\n Concatenated string is: %s”, target);

}

  • The strcmp() function

The strcmp function compares two strings and produces the result to store in a numeric variable. If both the strings are like, it produces 0 whereas 1 is produced when both strings are not alike.

Syntax: strcmp (string1, string2);

# Write a program to compare two strings.

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main ()

{

int st1;

char string1[] =”dog”;

char string2[] =”cat”;

char string3[] =”cat”;

int a, b;

printf(“\n String comparison strcmp ()”);

printf(“\n True results 0 and false results 1”);

a=strcmp (string1, string2);

b=strcmp (string2, string3);

printf(“\n String1 and string 2: %d”, a”);

printf(“\n String2 and string 3: %d”, b”);

}

# Write a program to input various sentences. Count number of words, sentences and characters in multiple text line.

#include <string.h>

#include <conio.h>

#include <string.h>

void main ()

{

char line[90], ctr;

int i, c, end=0, characters=0, words=0,lines =0;

printf(“\n Enter sentence. Press when finished.\n”);

while(end==0)

{

/*reading line of text */

c=0;

while ((ctr=getchar ())!=’\n’)

line]c++]=ctr;

line[c] =’\0’;

/*counting the word in the text */

if (line[0] ==’\0’)

break;

else

{

words++;

for (i=0; line[i] !=’\0’; i++)

if (line[i] ==’ ‘ || line [i] ==’\t’)

words++;

}

/* counting lines and characters */

lines = lines +1;

characters = characters+strlen(line);

}

printf(“\n”);

printf(“\n Number of lines = %d”, lines);

printf(“\n Number of words = %d”, words);

printf(“\n Number of characters = %d”, characters);

}

# Write a program to count number of words, lines and characters.

#include <stdio.h>

#include <conio.h>

#include <string.h>

main ()

{

char line[91], ctr;

int i, c, end=0, characters =0, words =0, lines =0;

printf(“\n Enter a sentences :”);

while (end ==0)

{

/* reading a line of text */

c=0;

while ((ctr=getchar ())!=’\n’)

line[c++] =ctr;

line[c] =’\0’)

/* counting the words in the given line */

if (line[0] ==’\0’)

break;

else

{

words++;

for(i=0; line[i] !=’\0’; i++)

if(line[i] ==’ ‘ || line[i] ==’\t’)

words++;

}

/* counting number of lines and characters */

lines = lines +1;

characters = characters +strlen (line);

}

printf(“\n Number of lines %d”, lines);

printf(“\n Number of words %d”, words);

printf(“\n Number of characters %d”, characters);

}

References:

Khanal, R.C. Khanal, R.C. Computer Concept for XII. Pashupatigriha Marga, Thapathali, Kathmandu, Nepal: Ekta Books Distributors Pvt. Ltd., 2010. 242-248.

Adhikari, Deepak Kumar.,et.al., Computer Science XII,Asia Publication Pvt.Ltd

Lesson

Programming in C

Subject

Computer Science

Grade

Grade 12

Recent Notes

No recent notes.

Related Notes

No related notes.