Computer Science XI class
Thursday, January 17, 2019
Monday, October 29, 2018
Finding Length of string using user defined functions
#include<iostream>
using namespace std;
int u_strlen(char s[])
{int i;
for(i=0; s[i]!='\0'; i++); // starts from first character till last
return i; // return strlen
}
int main()
{
// declare the string
char name[40];
//accept the string
// cin>>name; // ignore space in between
cin.getline(name,40); // accept space also
cout<<name <<endl; // display string
int len=u_strlen(name);
cout<<"Length of String is "<<len<<endl;
//display string in reverse order
for(int k=len-1;k>=0;k--)
cout<<name[k]<<endl;
system("pause");
return 0;
}
using namespace std;
int u_strlen(char s[])
{int i;
for(i=0; s[i]!='\0'; i++); // starts from first character till last
return i; // return strlen
}
int main()
{
// declare the string
char name[40];
//accept the string
// cin>>name; // ignore space in between
cin.getline(name,40); // accept space also
cout<<name <<endl; // display string
int len=u_strlen(name);
cout<<"Length of String is "<<len<<endl;
//display string in reverse order
for(int k=len-1;k>=0;k--)
cout<<name[k]<<endl;
system("pause");
return 0;
}
String Manipulation (output Answer)
#include<iostream>
using namespace std;
void u_strmanip(char s[])
{int i;
for(i=0; s[i]!='\0'; i++) // starts from first character till last
{
if(isupper(s[i]))
s[i]=s[i]+1;
else
if(i%2!=0)
s[i]=s[i+1];
else
if(!isalnum(s[i]))
s[i]='*';
}
}
int main()
{
// declare the string
char name[40];
//accept the string
// cin>>name; // ignore space in between
cin.getline(name,40); // accept space also
// cout<<name <<endl; // display string
u_strmanip(name);
//display string in reverse order
cout<<name;
system("pause");
return 0;
}
Give output for
CoMpuTEr SciENce 2019
Answer
DMNuuUF *TcEFOc *0099
using namespace std;
void u_strmanip(char s[])
{int i;
for(i=0; s[i]!='\0'; i++) // starts from first character till last
{
if(isupper(s[i]))
s[i]=s[i]+1;
else
if(i%2!=0)
s[i]=s[i+1];
else
if(!isalnum(s[i]))
s[i]='*';
}
}
int main()
{
// declare the string
char name[40];
//accept the string
// cin>>name; // ignore space in between
cin.getline(name,40); // accept space also
// cout<<name <<endl; // display string
u_strmanip(name);
//display string in reverse order
cout<<name;
system("pause");
return 0;
}
Give output for
CoMpuTEr SciENce 2019
Answer
DMNuuUF *TcEFOc *0099
Thursday, September 27, 2018
Concatenating two strings - strcat() - string.h
#include<string.h>
#include<iostream>
using namespace std;
// Program to convert a string into lower case
main()
{
char str[100],str_small[30];
cout<<"Enter a String 1 ";
cin.getline(str,100);
cout<<"Enter a String 2 ";
cin.getline(str_small,100);
cout<<"\n Entered String 1 = " <<str;
cout<<"\n Entered String 2 = " <<str_small;
strcat(str,str_small);
cout<<"\n Changed String = " <<str;
}
#include<iostream>
using namespace std;
// Program to convert a string into lower case
main()
{
char str[100],str_small[30];
cout<<"Enter a String 1 ";
cin.getline(str,100);
cout<<"Enter a String 2 ";
cin.getline(str_small,100);
cout<<"\n Entered String 1 = " <<str;
cout<<"\n Entered String 2 = " <<str_small;
strcat(str,str_small);
cout<<"\n Changed String = " <<str;
}
changing string into lowercase - strlwr() - string.h
#include<string.h>
#include<iostream>
using namespace std;
// Program to convert a string into lower case
main()
{
char str[100];
cout<<"Enter a String ";
cin.getline(str,100);
cout<<"\n Original String = " <<str;
strlwr(str);
cout<<"\n Changed String = " <<str;
}
#include<iostream>
using namespace std;
// Program to convert a string into lower case
main()
{
char str[100];
cout<<"Enter a String ";
cin.getline(str,100);
cout<<"\n Original String = " <<str;
strlwr(str);
cout<<"\n Changed String = " <<str;
}
converting string into uppercase - strupr() - string.h
#include<string.h>
#include<iostream>
using namespace std;
// Program to convert a string into upper case
main()
{
char str[100];
cout<<"Enter a String ";
cin.getline(str,100);
cout<<"\n Original String = " <<str;
strupr(str);
cout<<"\n Changed String = " <<str;
}
#include<iostream>
using namespace std;
// Program to convert a string into upper case
main()
{
char str[100];
cout<<"Enter a String ";
cin.getline(str,100);
cout<<"\n Original String = " <<str;
strupr(str);
cout<<"\n Changed String = " <<str;
}
Comparing two strings - case senstive
#include<string.h>
#include<iostream>
using namespace std;
// Program to compare a string
main()
{
char str1[100], str2[100];
cout<<"Enter a String ";
cin.getline(str1,100);
cout<<"\n"<<str1;
cout<<"Enter a String ";
cin.getline(str2,100);
cout<<"\n "<<str2;
if(strcmpi(str1,str2)==0)
cout<<"\n Strings are equal";
else
cout<<"\n Strings are unequal";
}
#include<iostream>
using namespace std;
// Program to compare a string
main()
{
char str1[100], str2[100];
cout<<"Enter a String ";
cin.getline(str1,100);
cout<<"\n"<<str1;
cout<<"Enter a String ";
cin.getline(str2,100);
cout<<"\n "<<str2;
if(strcmpi(str1,str2)==0)
cout<<"\n Strings are equal";
else
cout<<"\n Strings are unequal";
}
Subscribe to:
Posts (Atom)