String Examples
1) Length of string
#include<iostream.h>
#include<conio.h>
void main()
{
int len=0;
char s[10];
clrscr();
cout<<"\n Enter the string: ";
cin>>s;
while(s[len]!='\0')
{
len++;
}
cout<<" Length = "<<len;
getch();
}
Here is the output :)))
2) Swaping two strings
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <malloc.h>
int main()
{
char first[100], second[100], *temp;
clrscr();
cout<<"Enter the first string: ";
cin>>first;
cout<<"Enter the second string: ";
cin>>second;
cout<<"\nBefore Swapping\n";
cout<<"\nFirst string: "<<first;
cout<<"\nSecond string: "<<second;
temp = (char*)malloc(100);
strcpy(temp,first);
strcpy(first,second);
strcpy(second,temp);
cout<<"\n\nAfter Swapping\n";
cout<<"\nFirst string: "<<first;
cout<<"\nSecond string: "<<second;
getch();
}
Here is the output :)))
3) Reverse String
#include<iostream.h>
#include<string.h>
#include<conio.h>
void main()
{
char arr[100];
clrscr();
cout<<"\n Enter a string to reverse :\n ";
cin>>arr;
strrev(arr);
cout<<"\n Reverse of entered string is :\n "<<arr;
getch();
}
Here is the output :)))
No comments:
Post a Comment