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

String Programs

Uploaded by

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

String Programs

Uploaded by

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

String Length

#include <stdio.h>

int main()

char s[100];

int i;

printf("Enter a string: ");

scanf("%s", s);

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

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

return 0;

String Copy
#include <stdio.h>

int main()

char s1[] = "Vignan", s2[100], i;

// Print the string s1

printf("string s1 : %s\n", s1);

// Execute loop till null found

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

// copying the characters by


// character to str2 from str1

s2[i] = s1[i];

s2[i] = '\0';

// printing the destination string

printf("String s2 : %s", s2);

return 0;

String Comparison:
#include<stdio.h>

int main() {

char str1[30], str2[30];

int i;

printf("\nEnter two strings :");

scanf("%c",&str1);

gets("%c",&str2);

i = 0;

while (str1[i] == str2[i] && str1[i] != '\0')

i++;
if (str1[i] > str2[i])

printf("str1 > str2");

else if (str1[i] < str2[i])

printf("str1 < str2");

else

printf("str1 = str2");

return (0);

String Concatenate

int main()

// Get the two Strings to be concatenated

char str1[100] = "Vignan", str2[100] = "IIT";

// Declare a new Strings

// to store the concatenated String

char str3[100];

int i = 0, j = 0;

printf("\nFirst string: %s", str1);

printf("\nSecond string: %s", str2);

// Insert the first string in the new string

while (str1[i] != '\0') {


str3[j] = str1[i];

i++;

j++;

// Insert the second string in the new string

i = 0;

while (str2[i] != '\0') {

str3[j] = str2[i];

i++;

j++;

str3[j] = '\0';

// Print the concatenated string

printf("\nConcatenated string: %s", str3);

return 0;

You might also like