Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

String in C Notes for GATE

The string is an important topic belonging to the world of programming and coding languages. And, when it comes to a competitive examination like GATE, you have to read the whole topic quite thoroughly. In this article, we have covered all the topics relevant to the string in C. We hope the notes for the CSE topics will help you understand this topic in a better way.

What is String in C?

A string in C is like an array of characters, pursued by a NULL character.

For example: char c[] = “c string”

String in C

Syntax to Define the String in C

char string_variable_name [array_size];

char c[] = “c string”

How to Declare a String?

There are two ways to declare a string in C language.

  1. By char array
  2. By string literal

1. By Char Array

char greeting[6] = {‘T’, ‘a’, ‘b’, ‘l’, ‘e’, ‘\0’};

2. By String Literal

If you follow the rule of array initialisation, you can write the above statement as follows:

char greeting[] = “Table”;

Following is the memory presentation of the above-defined string in C/C++ −

Memory presentation in String

Another Example of String Declaration in C

char s[5];

Here in this example we have declared a string of 5 characters.

String Declaration

Example of String in C Programming

String in C Programming

Function and Purpose of String:

  • strcpy(s1, s2); – It is used to copy string s2 into string s1.
  • strcat(s1, s2); – It is used to concatenate string s2 onto the end of string s1.
  • strlen(s1); – It is used to return the length of string.
  • strcmp(s1, s2); – It is used to analyse and compare the strings s1 and s2. Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
  • strchr(s1, ch); – It is used to discover the foremost event or occurrence of a specified character within the actual string.
  • strstr(s1, s2); – It is used to return a pointer to a character at the first index

Example of String By Using Above-mentioned Functions:

#include <stdio.h>

#include <string.h>

int main () {

char str1[12] = “John”;

char str2[12] = “Potter”;

char str3[12];

int len ;

/* copy str1 into str3 */

strcpy(str3, str1);

printf(“strcpy( str3, str1) : %s\n”, str3 );

/* concatenates str1 and str2 */

strcat( str1, str2);

printf(“strcat( str1, str2): %s\n”, str1 );

/* total length of str1 after concatenation */

len = strlen(str1);

printf(“strlen(str1) : %d\n”, len );

return 0;

}

Output:

strcpy( str3, str1) : John

strcat( str1, str2): JohnPotter

strlen(str1) : 10

Traverse String

There are two ways to traverse a string

  • By using the length of string
  • By using the null character.
  • Example of using the length of string

#include<stdio.h>

void main ()

{

char s[11] = “byjuslearning”;

int i = 0;

int count = 0;

while(i<11)

{

if(s[i]==’a’ || s[i] == ‘e’ || s[i] == ‘i’ || s[i] == ‘u’ || s[i] == ‘o’)

{

count ++;

}

i++;

}

printf(“The number of vowels %d”,count);

}

Output: The number of vowels : 3

  • Second Example: Using Null Character

#include<stdio.h>

void main ()

{

char s[11] = “byjuslearning”;

int i = 0;

int count = 0;

while(s[i] != NULL)

{

if(s[i]==’a’ || s[i] == ‘e’ || s[i] == ‘i’ || s[i] == ‘u’ || s[i] == ‘o’)

{

count ++;

}

i++;

}

printf(“The number of vowels %d”,count);

}

Output: The number of vowels: 3

Practice Problems – String in C

Q. Consider the following C program.

#include<stidio.h>

#include<string.h>

int main () {

char* c = “GATECSIT2017”;

char* p = c;

printf (“%d”, (int)strlen (c+2[p]-6[p]-1));

return 0;

}

The output of the program is ____________.


Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*