0% found this document useful (0 votes)
159 views

C String Function

The document discusses various string handling functions in C including: - strlen to get the length of a string - strcpy to copy one string to another - strcat to concatenate two strings - strcmp to compare two strings - Functions to convert strings to uppercase and lowercase It explains how to use these functions and includes examples of code using each function.

Uploaded by

amit malhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views

C String Function

The document discusses various string handling functions in C including: - strlen to get the length of a string - strcpy to copy one string to another - strcat to concatenate two strings - strcmp to compare two strings - Functions to convert strings to uppercase and lowercase It explains how to use these functions and includes examples of code using each function.

Uploaded by

amit malhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

String operations

• Length (number of characters in the string).


• Conversion of a String from Uppercase to Lowercase.
• Conversion of String from Lowercase to uppercase.
• Reversing a given string
• Concatenation (adding two are more strings)
• Comparing two strings.
• Copy (copies one string over another)
C library supports a large number of string handling functions to do all the operations.
it is essential to include string.h library header
file in the program.
C String function – strlen
strlen: The strlen() function calculates the length of a given string.The strlen() function is
defined in string.h header file. It doesn’t count null character ‘\0’.

Syntax: strlen(s1);

Return: This function returns the length of string passed in integer

It returns the length of the string without including end character (terminating


char ‘\0’).

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10];
int l;
gets(s1);
l=strlen();
printf(“%d”,l);
getch();
}
strlen vs sizeof
strlen returns you the length of the string stored in array, however sizeof returns
the total allocated size assigned to the array. So if I consider the above example
again then the following statements would return the below values.

strlen(str1) returned value 13.


sizeof(str1) would return value 20 as the array size is 20 (see the first statement in
main function).

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10];
gets(s1);
strlupr(s1);
puts(s1);
getch();
}

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10];
gets(s1);
strlwr(s1);
puts(s1);
getch();
}

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10];
gets(s1);
strlrev(s1);
puts(s1);
getch();
}

It copies the string str2 into string str1, including the end character (terminator
char ‘\0’).

#include <stdio.h>
#include <string.h>
int main()
{
char s1[30] = "string 1";
char s2[30] = "string 2 : I’m gonna copied into s1";
/* this function has copied s2 into s1*/
strcpy(s1,s2);
printf("String s1 is: %s", s1);
return 0;
}

strcpy: strcpy() is a standard library function in C/C++ and is used to copy one string to another.
In C it is present in string.h header file and in C++ it is present in cstring header file.

Syntax:
char* strcpy(char* dest, const char* src);
Paramters: This method accepts following paramters:

 dest: Pointer to the destination array where the content is to be copied.


 src: string which will be copied.
Return Value: After copying the source string to the destination string, the strcpy()
function returns a pointer to the destination string.
Below program explains different usages of this library function:

filter_none
edit
play_arrow
brightness_4
// C program to illustrate

// strcpy() function ic C/C++

#include <stdio.h>

#include <string.h>

  

int main()

    char str1[] = "Hello Geeks!";

    char str2[] = "GeeksforGeeks";

    char str3[40];

    char str4[40];
    char str5[] = "GfG";

  

    strcpy(str2, str1);

    strcpy(str3, "Copy successful");

    strcpy(str4, str5);

    printf("str1: %s\nstr2: %s\nstr3: %s\nstr4: %s\n",

           str1, str2, str3, str4);

    return 0;

Output:

str1: Hello Geeks!


str2: Hello Geeks!
str3: Copy successful
str4: GfG

It concatenates two strings and returns the concatenated string.


strcat: The strcat() function will append a copy of the source string to the end of destination
string. The strcat() function takes two arguments:
1) dest
2) src
It will append copy of the source string in the destination string. 

When you combine two strings, you add the characters of one string to the end of
other string. This process is called concatenation. The strcat() function joins 2 strings
together.
It takes the following form :
strcat (string1, string2)
string1 & string2 are character arrays. When the function strcat is executed string2 is
appended to end of string1. The string at string2 remains unchanged.
#include <stdio.h>
#include <string.h>
int main()
{
char s1[10] = "Hello";
char s2[10] = "World";
strcat(s1,s2);
printf("Output string after concatenation: %s", s1);
return 0;
}
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
    char dest[50] = "This is an";
    char src[50] = " example";
  
    strcat(dest, src);
    cout << dest;
    return 0;
}

strncat: In C/C++, strncat() is a predefined function used for string handling. string.h is the
header file required for string functions.

This function appends not more than n characters from the string pointed to by src to
the end of the string pointed to by dest plus a terminating Null-character. The initial
character of string(src) overwrites the Null-character present at the end of string(dest).
Thus, length of the string(dest) becomes strlen(dest)+n. But, if the length of the
string(src) is less than n, only the content up to the terminating null-character is copied
and length of the string(dest) becomes strlen(src) + strlen(dest).

The behavior is undefined if


 the strings overlap.
 the dest array is not large enough to append the contents of src.

It compares the two strings and returns an integer value. If both the strings are
same (equal) then this function would return 0 otherwise it may return a negative
or positive value based on the comparison.

If string1 < string2 OR string1 is a substring of string2 then it would result in


a negative value. If string1 > string2 then it would return positive value.
If string1 == string2 then you would get 0(zero) when you use this function for
compare strings.

nt strcmp(const char *leftStr, const char *rightStr );


In the above prototype, function srtcmp takes two strings as parameters and returns an
integer value based on the comparison of strings.
 strcmp() compares the two strings lexicographically means it starts
comparison character by character starting from the first character until the
characters in both strings are equal or a NULL character is encountered.
 If first character in both strings are equal, then this function will check the second
character, if this is also equal then it will check the third and so on
 This process will be continued until a character in either string is NULL or the
characters are unequal.
filter_none
edit
play_arrow
brightness_4
// C program to illustrate
// strcmp() function
#include <stdio.h>
#include <string.h>
  
int main()
{
  
    char leftStr[] = "g f g";
    char rightStr[] = "g f g";
  
    // Using strcmp()
    int res = strcmp(leftStr, rightStr);
  
    if (res == 0)
        printf("Strings are equal");
    else
        printf("Strings are unequal");
  
    printf("\nValue returned by strcmp() is:  %d", res);
    return 0;
}
Output:
Strings are equal
Value returned by strcmp() is: 0

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10], s2[10];
gets(s1);
strcpy(s2,s1);
puts(s2);
getch();
}

You might also like