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

Lab5 Strings

The document shows C++ code examples for string manipulation functions including copying, concatenating, comparing and extracting substrings from strings. It demonstrates the use of strcpy(), strcat(), strncat(), strcmp(), strncmp() and related functions on various string examples.

Uploaded by

Nardos Tesema
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lab5 Strings

The document shows C++ code examples for string manipulation functions including copying, concatenating, comparing and extracting substrings from strings. It demonstrates the use of strcpy(), strcat(), strncat(), strcmp(), strncmp() and related functions on various string examples.

Uploaded by

Nardos Tesema
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<iostream>

#include<string.h>

using namespace std;

int main()

const int max=80;

char str1[]=" Oh, Captain, my Captain!"

"our fearful trip is done";

// cout<<"length= "<<strlen(str1);

char str2[max];

for(int i=0; i<strlen(str1);i++)

str2[i]=str1[i];

str2[50]='\0';

cout<<endl;

cout<<str2<<endl;

//........................................

char me[20] = "David";

cout << me << endl;

strcpy(me, "YouAreNotMe");

cout << me << endl ;

// .........................................

cout<<"___________________________________________"<<endl;

cout<<"this is sting copying "<<endl;

char str4[] = "String test";


char str5[] = "Hello";

char one[10];

strncpy(one, str4, 9);

one[9] = '\0';

cout << one << endl;

strncpy(one, str5, 2);

cout << one << endl;

strcpy(one, str5);

cout << one << endl;

// ....................................................

cout<<"_____________________________________"<<endl;

cout<<"This is concatenating strings ...."<<endl;

char str6[30];

strcpy(str6, "abc");

cout << str6 << endl;

strcat(str6, "def");

cout << str6 << endl;

char str7[] = "xyz";

strcat(str6, str7);

cout << str6 << endl;

str6[4] = '\0';

cout << str6 << endl;


// ...................................................

cout<<"_____________________________________________"<<endl;

cout<<"THis is string concatenating with a certain size"<<endl;

char str8[30];

strcpy(str8, "abc");

cout << str8 << endl;

strncat(str8, "def", 2);

str8[5] = '\0';

cout << str8 << endl;

char str9[] = "xyz";

strcat(str8, str9);

cout << str8 << endl;

str8[4] = '\0';

cout << str8 << endl;

// .........................................................

cout<<"_____________________________________________"<<endl;

cout<<"THis is string comparison with a certain size"<<endl;

cout << strcmp("abc", "def") << endl;

cout << strcmp("def", "abc") << endl;

cout << strcmp("abc", "abc") << endl;

cout << strcmp("abc", "abcdef") << endl;

cout << strcmp("abc", "ABC") << endl;

// ..........................................................

cout<<"string comparison of n characters"<<endl;

cout << strncmp("abc", "def", 2) << endl;


cout << strncmp("abc", "abcdef", 3) << endl;

cout << strncmp("abc", "abcdef", 2) << endl;

cout << strncmp("abc", "abcdef", 5) << endl;

cout << strncmp("abc", "abcdef", 20) << endl;

char str11[20];

cout<<"this is another thing "<<strcpy(str11,"string2")<<endl;

cout<<strcpy(str11,"mis")<<endl;

cout<<str11;

You might also like