0% found this document useful (0 votes)
40 views3 pages

Lab Material For Practice On String

This document discusses string handling functions in C including declaration, input/output, length, copying, concatenation, comparison, passing as arguments, and problems to implement functions for reversing, comparing, counting vowels/consonants, and replacing characters in strings.
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)
40 views3 pages

Lab Material For Practice On String

This document discusses string handling functions in C including declaration, input/output, length, copying, concatenation, comparison, passing as arguments, and problems to implement functions for reversing, comparing, counting vowels/consonants, and replacing characters in strings.
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/ 3

CSE115L – Computing Concepts Lab

String declaration, input and output:

#include<stdio.h>
int main() #include<stdio.h>
{ int main()
char {
str[5]={'E','X','I','T','\0'}; char str[10];
char st[]="world"; int i;
printf("%s\n",str); for(i=0;i<5;i++)
printf("%s",st); {
return 0; scanf("%c",&str[i]);
} }

#include<stdio.h> for(i=0;i<5;i++)
int main() {
{ printf("%c",str[i]);
char str[10]; }
char s[10];
gets(str); return 0;
scanf("%s",s); }
puts(str);
printf("%s\n",s);
return 0;
}

String length, string copying, string concatenation and string comparison:

#include<stdio.h> #include<stdio.h>
#include<string.h> #include<string.h>
int main()
{ int main()
char str1[10]; {
char str2[10]; char str1[10];
char str3[20]; char str2[10];
int len; char str3[20];
int chk;
gets(str1);
gets(str2); gets(str1);
gets(str2);
/* copy str1 into str3 */
strcpy(str3,str1); chk = strcmp(str1,str2);
printf("strcpy(str3,str1): printf("%d",chk);
%s\n",str3); if(chk == 0)
printf("Same");
/* concatenates str1 and str2 */ if(chk < 0)
strcat(str1,str2); printf("str1 is
printf("strcat(str1,str2): smaller");
%s\n",str1); if(chk>0)
printf("str2 is
/* total length of str1 after smaller");
concat */ return 0;
len=strlen(str1); }
printf("strlen(str1) : %d\n",
len);

return 0;
}
Passing string as function argument:

#include<stdio.h> void printString(char s[])


void printString(char s[]); {
int main() int i=0;
{ while(s[i]!='\0')
char str1[10]; {
gets(str1); printf("%c",s[i]);
printString(str1); i++;
return 0; }
} }

Problems:

1. Write a function that returns the length of a string without using the strlen() library
function. Take string as input in main and pass it to the function.

int length(char arr[]);

Sample Output:

Enter string: hello world


Length is: 11

2. Write a function that searches for a character in a string. The function should print true if
found false otherwise.

void search(char arr[], char key);

Sample Output 1: Sample Output 2:

Enter String: bangladesh Enter String: bangladesh


Search Key: g Search Key: v
Found Not found

3. Write a function that shows the number of vowels and consonants in a string.

void count(char arr[]);

Sample Output 1: Sample Output 2:

Enter string: Bangladesh Enter string: Programming


Vowels: 3 Vowels: 3
Consonants: 7 Consonants: 8
4. Implement the following function which reverses a string without using library function.

void reverse(char arr[]);

Take string as input in main and pass it to the reverse function.

Sample Output: Sample Output:

Enter String:logical
string: logical Enter string: madam
Reverse order:
order:lcaigol
lacigol Reverse order: madam

5. Implement the following function which compares two strings without using library
function.

int compare(char str1[], char str2[]);

The compare function returns 0 if str1 is equal to str2, returns 1 if str1 is greater
than str2 and returns -1 if str2 is greater than str1.

Sample Output 1: Sample Output 2:

Enter str1: Simple Enter str1: Normal


Enter str2: Temple Enter str2: Hard
-1 1

6. Implement the following function which replaces all the occurrences of one character
with another character in a string and shows the modified string.

void Replace(char arr[],char oldChar, char newChar);

The Replace function replaces all the occurrences of oldChar is with newChar in
the string arr.

Sample Output 1: Sample Output 2:

Enter string: Logical Enter string: University


Old char: g Old char: i
New char: n New char: e
Modified string: Lonical Modified string: Uneversety

You might also like