Lab Material For Practice On String
Lab Material For Practice On String
#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;
}
#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:
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.
Sample Output:
2. Write a function that searches for a character in a string. The function should print true if
found false otherwise.
3. Write a function that shows the number of vowels and consonants in a string.
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.
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.
6. Implement the following function which replaces all the occurrences of one character
with another character in a string and shows the modified string.
The Replace function replaces all the occurrences of oldChar is with newChar in
the string arr.