FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
1.4k views
in RTU/BTU B.Tech(CSE-I & II Sem) Computer Programming Lab by Goeduhub's Expert (7.6k points)
Programs to learn about string operations.

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited by
 
Best answer

Programs to learn about string operations.

String : Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’.

Declaration of strings:

Syntax : char str_name[size];

Example : char s[10];

Initialisation of strings :  

char str[] = "hello";

char str[50]="GoEduHub"

Program:  

#include <stdio.h>

#include<conio.h>

void main() {

char s[] = "GoEduHub";

int i;

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

printf("Length of the string: %d", i);

}

Output : 

Length of the string: 8


Functions of string :

S.noFunctionPurpose
1strcpy(s1, s2);Copies string s2 into string s1.
2strcat(s1, s2);Concatenates string s2 onto the end of string s1.
3strlen(s1);Returns the length of string s1.
4strcmp(s1,s2);
Returns 0 if s1 & s2 are  same; less than 0 if s1<s2; greater than 0 if s1>s2.
5strchr(s1,ch);Returns a pointer to  first occurrence of character ch in string s1.
6strstr(s1, s2);Returns a pointer to the first occurrence of string s2 in string s1.

Program : 

#include<stdio.h>

#include<conio.h>

int main () {

   char str1[12] = "Goeduhub";

   char str2[12] = "Technologies";

   char str3[12];

   int  len ;

   char ch = 'e'; 

   char* val; 

   char* p; 

  

    // Use of strrchr() 

    // returns "ks" 

    val = strrchr(str1, ch); 

  

    printf("String after last %c is :  %s \n",  ch, val);

   // 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 lenghth of str1 after concatenation 

   len = strlen(str1);

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

   

   //comparision of strings

   printf("comparision of strings : %d\n",strcmp(str1,str2));

   

   //use of strstr()

   p = strstr(str1, str2);  

    if (p) { 

        printf("String found\n"); 

        printf("First occurrence of string '%s' in '%s' is '%s'", str2, str1, p); 

    } else

        printf("String not found\n");

   return 0;

}

Output : 

String after last e is :  eduhub

strcpy( str3, str1) :  Goeduhub

strcat( str1, str2):   GoeduhubTechnologies

strlen(str1) :  20

comparision of strings : -1

String found

First occurrence of string 'Technologies' in 'GoeduhubTechnolo°²b' is 'Technolo°²b'


For more RTU/BTU II Sem Computer Programming Lab  Experiments CLICK HERE

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...