The document contains code snippets for string manipulation programs including:
1) Accepting a string and displaying it using functions
2) Accepting a string and displaying it in reverse
3) Accepting a string and displaying alternate characters
The programs demonstrate various string operations like substring extraction, concatenation of multiple strings, checking for character presence, and counting vowels. Functions and pointers are used to pass strings between functions and manipulate string characters.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online from Scribd
The document contains code snippets for string manipulation programs including:
1) Accepting a string and displaying it using functions
2) Accepting a string and displaying it in reverse
3) Accepting a string and displaying alternate characters
The programs demonstrate various string operations like substring extraction, concatenation of multiple strings, checking for character presence, and counting vowels. Functions and pointers are used to pass strings between functions and manipulate string characters.
The document contains code snippets for string manipulation programs including:
1) Accepting a string and displaying it using functions
2) Accepting a string and displaying it in reverse
3) Accepting a string and displaying alternate characters
The programs demonstrate various string operations like substring extraction, concatenation of multiple strings, checking for character presence, and counting vowels. Functions and pointers are used to pass strings between functions and manipulate string characters.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online from Scribd
The document contains code snippets for string manipulation programs including:
1) Accepting a string and displaying it using functions
2) Accepting a string and displaying it in reverse
3) Accepting a string and displaying alternate characters
The programs demonstrate various string operations like substring extraction, concatenation of multiple strings, checking for character presence, and counting vowels. Functions and pointers are used to pass strings between functions and manipulate string characters.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online from Scribd
Download as txt, pdf, or txt
You are on page 1of 22
String Questions : 1.
Program to Accept a String and display Using Functions/Arr
ays 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 St ring 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 charac ters 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 t o 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. Pro gram to Accept a String and a character and find out whether this character is p resent 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 ma ny times this Character occurs 14. Accept 2 strings and check whether all Charac ters 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 substrin g 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 str ing(strcpy) 19. Accept 2 strings and create third string which will be concatena tion of 2 strings ex : string3 = string1 + string2 20. Accept Multiple strings f rom user and display 21. Accept Multiple strings from user and display odd posit ioned 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 string s in either case 25. Accept Multiple strings and display combination of every tw o strings *------------------------*----------------------------*--------------- -------*------------------------------*------------------->>>>>>1) /*program to accept a string & display*/ #include<stdio.h> #include<conio.h> char string(char str2[10]); void main() { in t i; char str1[10]; clrscr(); printf("\n\nEnter the string\t"); for(i=0;i<=9;i++ ) { scanf("%c",&str1[i]); } string(str1); getch(); } char string(char str2[10]) { int j; printf("\n\nThe string is\t"); for(j=0;j<=9;j++) { printf("%c",str2[j]) ; } return 1; } ________________________________________________________ >>>>>>> >2) /* Program to accept a string and display in reverse using functions/arrays and pointers */ #include<stdio.h> #include<conio.h> #include<string.h> char reve rse(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 an d display its alternate characters */ #include<stdio.h> #include<conio.h> #inclu de<string.h> 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<=l-1;j=j+2) { printf("%c",p[j]); } return 1; } _____________________ _________________________________________ >>>>>>4) /*Program to accept a string and display alternate characters in either case*/ #include<stdio.h> #include<con io.h> #include<string.h> char altcase(char *p); void main() { char str[20]; clrs cr(); printf("\n\nEnter the string\t"); gets(str); altcase(str); getch(); } char altcase(char *p) { printf("\n\nAlternate characters of string ar e\t"); while(*p) { if(*p>='A' && *p<='Z') { *p=*p+32; } else if(*p>='a' && *p<=' z') { *p=*p-32; } printf("%c",*p); p+=2; } return 1; } _________________________ ________________________________________ >>>>>5) /*Program to accept a string an d display its substring*/ #include<stdio.h> #include<conio.h> char substr(char * p,int s,int n); void main() { char str[10]; int start,no_of_chars; clrscr(); pri ntf("\n\nEnter the string\t"); gets(str); printf("\n\nEnter start position for s ubstring\t"); fflush(stdin); scanf("%d",&start); printf("\n\nEnter number of cha racters\t"); fflush(stdin); scanf("%d",&no_of_chars); substr(str,start,no_of_cha rs); getch(); } char substr(char *p,int s,int n) { int i; printf("\n\nThe substr ing is\t"); for(i=s-1;i<n;i++) { printf("%c",p[i]); } return 1; } ______________ ___________________________________________________ >>>>>>6) /*Program to accept a string and display its alternate characters in re verse*/ #include<stdio.h> #include<conio.h> #include<string.h> 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 strin g*/ #include<stdio.h> #include<conio.h> #include<string.h> void largstr(char *p1 ,char *p2); void main() { char str1[10]; char str2[10]; clrscr(); printf("\n\nEn ter first string\t"); fflush(stdin); gets(str1); printf("\n\nEnter second string \t"); fflush(stdin); gets(str2); largstr(str1,str2); } void largstr(char *p1,cha r *p2) { int l1,l2,i; l1=strlen(p1); l2=strlen(p2); printf("\n\nThe largest stri ng is\t"); if(l1>l2) { for(i=0;i<l1;i++) { printf("%c",p1[i]); } } else { for(i=0;i<l2;i++) { printf("% c",p2[i]); } } getch(); } ______________________________________________________ _____________________ >>>>>>8) /*Program to accept two strings and display combi nation of two strings*/ #include<stdio.h> #include<conio.h> #include<string.h> v oid 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\nEn ter second string\t"); fflush(stdin); gets(str2); comb(str1,str2); } void comb(c har *p1,char *p2) { int i,l1,l2; l1=strlen(p1); l2=strlen(p2); printf("\n\nThe f irst string is\t"); for(i=0;i<l1;i++) { printf("%c",p1[i]); } printf("\n\nThe se cond string is\t"); for(i=0;i<l2;i++) { printf("%c",p2[i]); } getch(); } _______ _______________________________________________________________________ >>>>>>9) /* Program to accept a string and display vowels */ #include<stdio.h> #include<c onio.h> #include<string.h> char vowels(char *p); void main() { char str[10]; clr scr(); 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 ar e\t"); for(i=0;i<l;i++) { if(p[i]=='a' || p[i]== 'e' || p[i]=='i' || p[i]=='o' | | p[i]=='u'|| p[i]=='A' || p[i]=='E' || p[i]=='I' || p[i]=='O' || p[i]=='U') { p rintf("%c",p[i]); } else continue; } return 1; } _______________________________ ___________________________________________________ _ >>>>>>10) /* Program to ac cept a string and display number of vowels */ #include<stdio.h> #include<conio.h > #include<string.h> char vowels(char *p); void main() { char str[10]; clrscr(); printf("\n\nEnter the string\t"); gets(str); vowels(str); getch(); } char vowel s(char *p) { int i,l,v=0; l=strlen(p); printf("\n\nThe number of vowels in the s tring is\t"); for(i=0;i<l;i++) { if(p[i]=='a' || p[i]== 'e' || p[i]=='i' || p[i]=='o' || p[i]= ='u'|| p[i]=='A' || p[i]=='E' || p[i]=='I' || p[i]=='O' || p[i]=='U') { v++; } e lse continue; } printf("%d",v); return 1; } ____________________________________ ______________________________________________ ___ >>>>>>11) /* Program to accep t a string and display number of each vowels */ #include<stdio.h> #include<conio .h> #include<string.h> char vowels(char *p); void main() { char str[10]; clrscr( ); printf("\n\nEnter the string\t"); gets(str); vowels(str); getch(); } char vow els(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=str len(p); printf("\n\nThe number of vowels in the string is\t"); for(i=0;i<l;i++) { if(p[i]=='a') { v1++; } if(p[i]=='e') { v2++; } if(p[i]=='i') { v3++; } if(p[i ]=='o') { v4++; } if(p[i]=='u') { v5++; } if(p[i]=='A') { v6++; } if(p[i]=='E') { v7++; } if(p[i]=='I') { v8++; } if(p[i]=='O') { v9++; } if(p[i]=='U') { v10++; } else continue; } printf("a=%d e=%d i=%d o=%d u=%d A=%d E=%d I=%d O=%d U=%d",v1,v2,v3, v4,v5,v6,v7,v8,v9,v10); return 1; } ____________________________________________ ______________________________________ ____ >>>>>>12) /* Program to accept a str ing and find out whether this character is present in the string */ #include<std io.h> #include<conio.h> #include<string.h> 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<l;i++) { if(p[i ]==hc) { a++; } else continue; } if(a>=1) { printf("\n\nYes , the entered charac ter is present in the string"); } else { printf("\n\nNo , the entered character is not present in the string"); } getch(); } ___________________________________ _______________________________________________ ____ >>>>>>13) /* Program to acc ept a string and find out whether this character is present in the string if pre sent then display how many times this character occurs */ #include<stdio.h> #inc lude<conio.h> #include<string.h> void s(char *p,char void main() { char str[10]; char ch; clrscr(); printf("\n\nEnter gets(str); printf("\n\nEnter scanf("%c",&c h); s(str,ch); } void s(char *p,char { int a=0; int i,l; l=strlen(p); for(i=0;i< l;i++) { if(p[i]==hc) { hc); the string\t"); the character\t"); hc) } printf("\n\nThe Entered Character occurs %d times",a); getch(); } ____________ ______________________________________________________________________ ____ >>>> >>14) /* Accept two strings and check whether all characters from first string a re present in second string */ #include<stdio.h> #include<conio.h> #include<stri ng.h> 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); print f("\n\nEnter second string\t"); fflush(stdin); gets(str2); charprs(str1,str2); g etch(); } 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;i++) { printf("%c" ,p1[i]); } printf("\n\nThe second string is\t"); for(j=0;j<l2;j++) { printf("%c" ,p2[j]); } for(i=0;i<l1;i++) { for(j=0;j<l2;j++) { if(p1[i]==p2[j]) { a++; } els e a++; } else continue; continue; } if(l1>l2) { if(a>=l2) { printf("\n\nYes, all characters of any one o f both the strings are present in the other string"); } else { printf("\n\nNo, a ll characters of any one of both the strings are not present in the other string "); } } if(l2>l1) { if(a>=l1) { printf("\n\nYes, all characters of any one of bo th the strings are present in the other string"); } else { printf("\n\nNo, all c haracters of any one of both the strings are not present in the other string"); } } return 1; } ________________________________________________________________ __________________ ____ >>>>>>15) /* Accept two strings and check whether all ch aracters from first string are present in second string */ } #include<stdio.h> #include<conio.h> #include<string.h> void charprs(char *p1,cha r *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 *p 2) { 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<l1;i++) { printf("%c",p1[i]); } printf("\n\nThe second string is\t"); for(j=0;j<l2;j++) { printf("%c",p2[j]); } for(i=0;i<l1;i++) { a=0; b++; for(j=0;j<l2;j++) { if(p1[i]==p2[j]) { a++; } else continue; } printf("\n\n%d ch aracter is %d times",b,a); } getch(); } ________________________________________ __________________________________________ ____ >>>>>>16) /* Program to accept t wo strings and display the largest string */ #include<stdio.h> #include<conio.h> #include<string.h> void largstr(char *p1,char *p2); void main() { char str1[10] ; char str2[20]; clrscr(); printf("\n\nEnter First string\t"); fflush(stdin); ge ts(str1); printf("\n\nEnter Second string\t"); fflush(stdin); gets(str2); largst r(str1,str2); } void largstr(char *p1,char *p2) { int i=0,j=0,l1=0,l2=0; while(p 1[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 fo r(i=0;i<l1;i++) { printf("%c",p1[i]); } } else if(l2>l1) { printf("\n\nThe large st string for(j=0;j<l2;j++) { printf("%c",p2[j]); } } getch(); first string is % d",l1); second string is %d",l2); is\t"); is\t"); } ______________________________________________________________________________ ____ ____ >>>>>>17) /*Program to accept two strings and extract substring from b oth the strings. Create third string which will be substring1+substring2 */ #inc lude<stdio.h> #include<conio.h> void substr(char *p1,char *p2,char *p3,int s1,in t 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"); ffl ush(stdin); gets(str2); printf("\n\nEnter start position for first string\t"); f flush(stdin); scanf("%d",&start1); printf("\n\nEnter number of characters for fi rst 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,st art2,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<s1 +no1;i++) { printf("%c",p1[i]); } printf("\n\nThe second substring is\t"); for(j =s2;j<s2+no2;j++) { printf("%c",p2[j]); } printf("\n\nThe third string is\t"); f or(i=s1,k=0;i<s1+no1;i++,k++) { p3[k]=p1[i]; printf("%c",p3[k]); } printf("\t"); for(j=s2,l=0;j<s2+no2;j++,l++) { p3[l]=p2[j]; printf("%c",p3[l]); } getch(); } ________________________________________________________________________________ __ ____ >>>>>>18) /*Program to accept s string from user and copy it to another string*/ #include<stdio.h> #include<conio.h> #include<string.h> 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<l1;i ++) { printf("%c",p1[i]); } printf("\n\nThe second string 'copy of first string' is\t"); for(i=0,j=0;i<l1;i++,j++) { p2[j]=p1[i]; printf("%c",p2[j]); } getch(); } _____________________________________________________________________________ _____ ____ >>>>>>19) /* Program to accept two strings and create third string wh ich will be concatenation of two strings */ #include<stdio.h> #include<conio.h> #include<string.h> void substr(char *p1,char *p2,char *p3); void main() { char s tr1[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(stdi n); 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<l1;i++) { printf("%c",p1[i]); } printf("\n\nThe second string is\t"); for(j=0;j<l2;j++) { printf("%c",p2[j]); } printf("\n\nThe third string i s\t"); } ______________________________________________________________________________ ____ ____ >>>>>>20) /* Program to accept multiple strings from user and display */ #include<stdio.h> #include<conio.h> #include<string.h> char multstr(char *p1) ; void main() { char name[10]; int i; clrscr(); for(i=0;i<5;i++) { printf("\n\nE nter %d string\t",i+1); gets(name); multstr(name); getch(); } } char multstr(cha r *p1) { printf("\n\nString is %s",p1); return 0; } ____________________________ ______________________________________________________ ____ >>>>>>21) /* Program to accept multiple strings from user and display odd positioned strings */ #inc lude<stdio.h> #include<conio.h> void oddstr(char *p); struct name { char nm[10]; }; for(i=0,k=0;i<l1;i++,k++) { p3[k]=p1[i]; printf("%c",p3[k]); } printf("\t"); for (j=0,k=0;j<l2;j++,k++) { p3[k]=p2[j]; printf("%c",p3[k]); } getch(); void main() { char name[10]; int i,j=0,k; struct name n[5]; clrscr(); for(i=0;i< 5;i++) { j++; printf("\n\nEnter %d string\t",j); gets(n[i].nm); } for(k=0;k<5;k+ =2) { oddstr(n[k].nm); } } void oddstr(char *p) { printf("\n\n\nThe string is %s ",p); getch(); } _______________________________________________________________ ___________________ ____ >>>>>>22) /* Program to accept multiple strings and dis play even positioned strings in reverse #include<stdio.h> #include<conio.h> 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<5;i++) { j++; printf("\n\nEnt er %d string\t",j); gets(n[i].nm); } for(k=3;k>=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 posit ioned strings in reverse */ #include<stdio.h> #include<conio.h> 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<5;i++) { j++; printf("\n\nEnter %d string\t",j); gets(n[i].nm); } for (k=4;k>=0;k-=2) { altrevstr(n[k].nm); } } void altrevstr(char *p) { printf("\n\n \nThe string is %s",p); getch(); } _____________________________________________ _____________________________________ ____ >>>>>>24) /* Program to accept multip le strings and display alternate characters of alternate strings in either case */ #include<stdio.h> #include<conio.h> #include<string.h> void altcharstr(char *p); struct name { char nm[10]; }; void main() { char name[10]; int i,j=0,k; struct name n[5]; clrscr(); } void altcharstr(char *p) { int j,l; l=strlen(p); printf("\n\nEnter the string\ t"); for(j=0;j<l;j++) { if(p[j]>='A' && p[j]<='Z') { p[j]=p[j]+32; printf("%c",p [j]); } else if(p[j]>='a' && p[j]<='z') { p[j]=p[j]-32; printf("%c",p[j]); } get ch(); } } ______________________________________________________________________ ____________ ____ >>>>>>>25) /* Program to accept multiple strings and display c ombination of every two strings for(i=0;i<5;i++) { j++; printf("\n\nEnter %d string\t",j); gets(n[i].nm); } for( k=0;k<5;k+=2) { altcharstr(n[k].nm); } */ #include<stdio.h> #include<conio.h> #include<string.h> void altrevstr(char *p); struct name { char nm[10]; }; void main() { char name[10]; int i,j=0,k,m=0; stru ct name n[6]; clrscr(); for(i=0;i<6;i++) { j++; printf("\n\nEnter %d string\t",j ); gets(n[i].nm); } for(k=0;k<6;k++) { printf("\n\n\n"); altrevstr(n[k].nm); for(m=0;m<6;m++) { if(m !=k) { altrevstr(n[m].nm); } else continue; } } } void altrevstr(char *p) { { pr intf("\n\n\nThe string is %s",p); getch(); } } *_________________________*______ ________________________*