Some Important programs on Strings(in C)

January 13, 2017 | Author: Anik | Category: N/A
Share Embed Donate


Short Description

About 25 basic programs on strings in C language...

Description

String Questions : 1. Program to Accept a String and display Using Functions/Arrays and Pointers 2. Program to Accept a String and display in Reverse 3. Program to Accept a String and display its alternate characters 4. Program to Accept a String and display Alternate characters in either case 5. Program to Accept a String and display its substring(Accept parameters from user) ex. substr(str,start_pos,no_of_chars) 6. Program to Accept a String and display its alternate characters in reverse 7. Program to Accept 2 strings and display the largest string 8. Program to Accept 2 strings and display combination of two strings 9. Program to Accept a String and display vowels 10. Program to Accept a String and display no of vowels 11. Program to Accept a String and display no of each Vowel 12. Program to Accept a String and a character and find out whether this character is present in the string 13. Program to Accept a String and a Character and find out whether this character is present in the string. If Present then display how many times this Character occurs 14. Accept 2 strings and check whether all Characters from 1st string are present in 2nd String 15. Accept 2 strings and display how many times each character from 1st string occurs in 2nd string 16. Accept 2 strings and display the largset string 17. Accept 2 strings and extract substring from both the strings(Accept arguments from user) Create 3rd string which will be substring1+substring2 18. Accept String from user and copy it to another string(strcpy) 19. Accept 2 strings and create third string which will be concatenation of 2 strings ex : string3 = string1 + string2 20. Accept Multiple strings from user and display 21. Accept Multiple strings from user and display odd positioned strings 22. Accept Multiple strings and display even positioned strings in reverse 23. Accept Multiple strings and display alternate strings in reverse

24. Accept Multiple strings and display alternate characters of alternate strings in either case 25. Accept Multiple strings and display combination of every two strings *------------------------*----------------------------*----------------------*------------------------------*------------------->>>> >>>>>> >>1) 1)

/*pr /*prog ogra ram m to acce accept pt a stri string ng & disp displa lay* y*/ /

#include #include char string(char str2[10]); void main() { int i; char str1[10]; clrscr(); printf("\n\nEnter the string\t"); for(i=0;i>>>>>>2) /* Program to accept a string and display in reverse using functions/arrays and pointers */ #include #include #include char reverse(char *p); void main() { int i; char str[10]; clrscr(); printf("\n\nEnter the String\t"); gets(str); reverse(str); getch(); }

char reverse(char *p) { int j,l; l=strlen(p); printf("\n\nString in reverse is \t"); for(j=l-1;j>=0;j--) { printf("%c",p[j]); } return 1; } _________________________________________________________ >>>>>>>3) /* Program to accept a string and display its alternate characters */ #include #include #include char alt(char *p); void main() { char str[10]; clrscr(); printf("\n\nEnter the string\t"); gets(str); alt(str); getch(); } char alt(char *p) { int j,l; l=strlen(p); printf("\n\nAlternate characters of the entered string are\t"); for(j=0;j>>>>>4) /*Program to accept a string and display alternate characters in either case*/ #include #include #include char altcase(char *p); void main() { char str[20]; clrscr(); printf("\n\nEnter the string\t"); gets(str); altcase(str);

getch(); } char altcase(char *p) { printf("\n\nAlternate characters of string are\t"); while(*p) { if(*p>='A' && *p='a' && *p>>>>5) /*Program to accept a string and display its substring*/ #include #include char substr(char *p,int s,int n); void main() { char str[10]; int start,no_of_chars; clrscr(); printf("\n\nEnter the string\t"); gets(str); printf("\n\nEnter start position for substring\t"); fflush(stdin); scanf("%d",&start); printf("\n\nEnter number of characters\t"); fflush(stdin); scanf("%d",&no_of_chars); substr(str,start,no_of_chars); getch(); } char substr(char *p,int s,int n) { int i; printf("\n\nThe substring is\t"); for(i=s-1;i>>>>>6) /*Program to accept a string and display its alternate characters in reverse*/ #include #include #include char realt(char *p); void main() { char str[10]; clrscr(); printf("\n\nEnter the string\t"); gets(str); realt(str); getch(); } char realt(char *p) { int i,l; l=strlen(p); printf("\n\nAlternate characters in reverse\t"); for(i=l-1;i>=0;i-=2) { printf("%c",p[i]); } return 1; } ______________________________________________________________________ >>>>>>7) /*Program to accept two strings and display the largest string*/ #include #include #include void largstr(char *p1,char *p2); void main() { char str1[10]; char str2[10]; clrscr(); printf("\n\nEnter first string\t"); fflush(stdin); gets(str1); printf("\n\nEnter second string\t"); fflush(stdin); gets(str2); largstr(str1,str2); } void largstr(char *p1,char *p2) { int l1,l2,i; l1=strlen(p1); l2=strlen(p2); printf("\n\nThe largest string is\t"); if(l1>l2) {

for(i=0;i>>>>8) /*Program to accept two strings and display combination of two strings*/ #include #include #include void comb(char *p1,char *p2); void main() { char str1[10]; char str2[10]; clrscr(); printf("\n\nEnter first string\t"); fflush(stdin); gets(str1); printf("\n\nEnter second string\t"); fflush(stdin); gets(str2); comb(str1,str2); } void comb(char *p1,char *p2) { int i,l1,l2; l1=strlen(p1); l2=strlen(p2); printf("\n\nThe first string is\t"); for(i=0;i>>>>9)

/* Program to accept a string and display vowels */ #include #include #include char vowels(char *p); void main() { char str[10]; clrscr(); printf("\n\nEnter the string\t"); gets(str); vowels(str); getch(); } char vowels(char *p) { int i,l; l=strlen(p); printf("\n\nThe vowels in the string are\t"); for(i=0;i>>>>>10) /* Program to accept a string and display number of vowels */ #include #include #include char vowels(char *p); void main() { char str[10]; clrscr(); printf("\n\nEnter the string\t"); gets(str); vowels(str); getch(); } char vowels(char *p) { int i,l,v=0; l=strlen(p); printf("\n\nThe number of vowels in the string is\t");

for(i=0;i>>>>>11) /* Program to accept a string and display number of each vowels */ #include #include #include char vowels(char *p); void main() { char str[10]; clrscr(); printf("\n\nEnter the string\t"); gets(str); vowels(str); getch(); } char vowels(char *p) { int i,l,v1=0,v2=0,v3=0,v4=0,v5=0,v6=0,v7=0,v8=0,v9=0,v10=0; l=strlen(p); printf("\n\nThe number of vowels in the string is\t"); for(i=0;i>>>>>12) /*

Program to accept a string and find out whether this character is present in the string */

#include #include #include void s(char *p,char hc); void main() { char str[10]; char ch; clrscr(); printf("\n\nEnter the string\t"); gets(str); printf("\n\nEnter the character\t"); scanf("%c",&ch); s(str,ch); }

void s(char *p,char hc) { int a; int i,l; l=strlen(p); for(i=0;i=1) { printf("\n\nYes , the entered character is present in the string"); } else { printf("\n\nNo , the entered character is not present in the string"); } getch(); } __________________________________________________________________________________ ____ >>>>>>13) /* Program to accept a string and find out whether this character is present in the string if present then display how many times this character occurs */ #include #include #include void s(char *p,char void main() { char str[10]; char ch; clrscr(); printf("\n\nEnter gets(str); printf("\n\nEnter scanf("%c",&ch); s(str,ch); } void s(char *p,char { int a=0; int i,l; l=strlen(p); for(i=0;i>>>>>14) /* Accept two strings and check whether all characters from first string are present in second string */ #include #include #include int charprs(char *p1,char *p2); void main() { char str1[10]; char str2[20]; clrscr(); printf("\n\nEnter first string\t"); fflush(stdin); gets(str1); printf("\n\nEnter second string\t"); fflush(stdin); gets(str2); charprs(str1,str2); getch(); } int charprs(char *p1,char *p2) { int i,j,l1,l2,a=0; l1=strlen(p1); l2=strlen(p2); printf("\n\nThe first string is\t"); for(i=0;i=l1) { printf("\n\nYes, all characters of any one of both the strings are present in the other string"); } else { printf("\n\nNo, all characters of any one of both the strings are not present in the other string"); } } return 1; } __________________________________________________________________________________ ____ >>>>>>15) /*

Accept two strings and check whether all characters from first string are present in second string */

#include #include #include void charprs(char *p1,char *p2); void main() { char str1[10]; char str2[20]; clrscr(); printf("\n\nEnter first string\t"); fflush(stdin); gets(str1); printf("\n\nEnter second string\t"); fflush(stdin); gets(str2); charprs(str1,str2); } void charprs(char *p1,char *p2) {

int i,j,l1,l2,a,b=0; l1=strlen(p1); l2=strlen(p2); printf("\n\nThe first string is\t"); for(i=0;i>>16) /* Program to accept two strings and display the largest string */ #include #include #include void largstr(char *p1,char *p2); void main() { char str1[10]; char str2[20]; clrscr(); printf("\n\nEnter First string\t"); fflush(stdin); gets(str1); printf("\n\nEnter Second string\t"); fflush(stdin); gets(str2); largstr(str1,str2); } void largstr(char *p1,char *p2) { int i=0,j=0,l1=0,l2=0; while(p1[i]!='\0')

{ i++; l1++; } printf("\n\nThe string length of while(p2[j]!='\0') { j++; l2++; } printf("\n\nThe string length of if(l1>l2) { printf("\n\nThe largest string for(i=0;il1) { printf("\n\nThe largest string for(j=0;j>>>>>17) /*Program to accept two strings and extract substring from both the strings. Create third string which will be substring1+substr substring1+substring2 ing2 */ #include #include void substr(char *p1,char *p2,char *p3,int s1,int no1,int s2,int no2); void main() { char str1[10]; char str2[12]; char str3[25]; int start1,start2,no_of_chars1,no_of_chars2; clrscr(); printf("\n\nEnter first string\t"); fflush(stdin); gets(str1); printf("\n\nEnter second string\t"); fflush(stdin); gets(str2); printf("\n\nEnter start position for first string\t"); fflush(stdin); scanf("%d",&start1); printf("\n\nEnter number of characters for first string\t"); fflush(stdin); scanf("%d",&no_of_chars1);

printf("\n\nEnter start position for second string\t"); fflush(stdin); scanf("%d",&start2); printf("\n\nEnter number of characters for second string\t"); fflush(stdin); scanf("%d",&no_of_chars2); substr(str1,str2,str3,start1,no_of_chars1,start2,no_of_chars2); } void substr(char *p1,char *p2,char *p3,int s1,int no1,int s2,int no2) { int i,j,k,l; printf("\n\nThe first substring is\t"); for(i=s1;i>>18) /*Program to accept s string from user and copy it to another string*/ #include #include #include void substr(char *p1,char *p2); void main() { char str1[10]; char str2[10]; clrscr(); printf("\n\nEnter first string\t"); fflush(stdin); gets(str1); substr(str1,str2); } void substr(char *p1,char *p2)

{ int i,j,l1; l1=strlen(p1); printf("\n\nThe first string is\t"); for(i=0;i>>>>19) /* Program to accept two strings and create third string which will be concatenation of two strings */ #include #include #include void substr(char *p1,char *p2,char *p3); void main() { char str1[10]; char str2[12]; char str3[25]; clrscr(); printf("\n\nEnter first string\t"); fflush(stdin); gets(str1); printf("\n\nEnter second string\t"); fflush(stdin); gets(str2); substr(str1,str2,str3); } void substr(char *p1,char *p2,char *p3) { int i,j,l1,l2,k; l1=strlen(p1); l2=strlen(p2); printf("\n\nThe first string is\t"); for(i=0;i>>20) /* Program to accept multiple strings from user and display */ #include #include #include char multstr(char *p1); void main() { char name[10]; int i; clrscr(); for(i=0;i>>>>>21) /* Program to accept multiple strings from user and display odd positioned strings */ #include #include void oddstr(char *p); struct name { char nm[10]; };

void main() { char name[10]; int i,j=0,k; struct name n[5]; clrscr(); for(i=0;i>>>>22) /* Program to accept multiple strings and display even positioned strings in reverse #include #include void evnrevstr(char *p); struct name { char nm[10]; }; void main() { char name[10]; int i,j=0,k; struct name n[5]; clrscr(); for(i=0;i=0;k-=2) { evnrevstr(n[k].nm); } } void evnrevstr(char *p) { printf("\n\n\nThe string is %s",p); getch();

*/

} __________________________________________________________________________________ ____ >>>>>>23) /*

Program to accept multiple strings and display even positioned strings in reverse

*/

#include #include void altrevstr(char *p); struct name { char nm[10]; }; void main() { char name[10]; int i,j=0,k; struct name n[5]; clrscr(); for(i=0;i=0;k-=2) { altrevstr(n[k].nm); } } void altrevstr(char *p) { printf("\n\n\nThe string is %s",p); getch(); } __________________________________________________________________________________ ____ >>>>>>24) /*

Program to accept multiple strings and display alternate characters of alternate strings in either case */

#include #include #include void altcharstr(char *p); struct name { char nm[10]; }; void main() { char name[10]; int i,j=0,k; struct name n[5]; clrscr();

for(i=0;i>>>>25) /*

Program to accept multiple strings and display combination of every two strings

#include #include #include void altrevstr(char *p); struct name { char nm[10]; }; void main() { char name[10]; int i,j=0,k,m=0; struct name n[6]; clrscr(); for(i=0;i
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF