Strings
[SET – 1]
1
|
#include<iostream.h> #include<conio.h> #include<stdio.h> int main( ) { clrscr( ); char str[80]; cout<<"Enter a string:"; gets(str); for(int i=0;str[i]!='\0';i++); cout<<"Lenght of string is :"<<i; getch(); return 0; } |
2
|
#include<iostream.h> #include<conio.h> #include<stdio.h> int main( ) { clrscr( ); char str[80]; cout<<"Enter a string:"; gets(str); for(int l=0; str[l]!='\0';l++); //Loop to find the length of the string for(int i=l-1;i>=0;i--) //Loop to display the string backwards cout<<str[i]; getch(); return 0; } |
3
|
#include<iostream.h> #include<conio.h> #include<stdio.h> int main( ) { clrscr( ); char str[80]; int words=0; cout<<"Enter a string:"; gets(str); for(int i=0;str[i]!='\0';i++) { if (str[i]==' ') words++; //Checking for spaces } cout<<"The number of words="<<words+1<<endl; getch(); return 0; } |
4
|
#include<iostream.h> #include<conio.h> #include<stdio.h> int main( ) { clrscr( ); char str1[80],str2[80]; cout<<"Enter first string :"; gets(str1); cout<<"Enter second string :"; gets(str2); for(int l=0;str1[l]!='\0';l++); for(int i=0;str2[i]!='\0';i++) str1[l++]=str2[i]; str1[l]='\0'; cout<<"\nThe first string after adding second string content is\n\n"<<str1; getch(); return 0; } |
5
|
#include<iostream.h> #include<conio.h> #include<stdio.h> int main( ) { clrscr( ); char STR1[80],STR2[80]; cout<<"Enter first string:"; gets(STR1); cout<<"Enter second string:"; gets(STR2); for (int I=0; STR1[I]==STR2[I] && STR1[I]!= '\0' && STR2[I] != '\0'; I++); if(STR1[I]-STR2[I]==0) cout<<"Strings are equal"; else cout<<"Strings are not equal"; getch(); return 0; } |
6
|
#include<iostream.h> #include<conio.h> #include<stdio.h> int main( ) { clrscr( ); char str[80]; cout<<"Enter a string:"; gets(str); for(int L=0;str[L]!='\0';L++); //To find length of the string for(int C=0;(C<L/2) && (str[C]==str[L-C-1]);C++); if(C==L/2) cout<<"Palindrome"; else cout<<"Not a palindrome"; getch(); return 0; } |
7
|
#include<iostream.h> #include<conio.h> #include<stdio.h> int main( ) { clrscr( ); char str1[80],str2[80]; cout<<"Enter first string :"; gets(str1); cout<<"Enter second string :"; gets(str2); for(int l=0;str2[l]!='\0';l++); //finding length of string 2 for(int i=0,j=0;str1[i]!='\0'&& str2[j]!='\0';i++) if(str1[i]==str2[j]) j++; else j=0; if(j==l) cout<<"Substring found at position "<<i-j+1; else cout<<"Substring not found"; getch(); return 0; } |
8
|
#include<iostream.h> #include<conio.h> #include<stdio.h> int main( ) { clrscr( ); char str[80]; int temp; cout<<"Enter string :"; gets(str); for(int l=0;str[l]!='\0';l++); //finding length of string for(int i=0,j=l-1;i<l/2;i++,j--) { temp=str[i]; str[i]=str[j]; str[j]=temp; } cout<<"Reverse String is\n"<<str; getch(); return 0; } |
9
|
#include<iostream.h> #include<conio.h> #include<stdio.h> int main( ) { clrscr( ); char str[80]; cout<<"Enter a string:"; gets(str); for(int i=0;str[i]!='\0';i++) str[i] = (str[i]>='A' && str[i]<='Z')?(str[i]+32):str[i]; cout<<str; getch(); return 0; } |
10
|
#include<iostream.h> #include<conio.h> #include<stdio.h> int main( ) { clrscr( ); char str[80]; cout<<"Enter a string:"; gets(str); for(int i=0;str[i]!='\0';i++) str[i] = (str[i]>='a' && str[i]<='z')?(str[i]-32):str[i]; cout<<str; getch(); return 0; } |
No comments:
Post a Comment