Vous êtes sur la page 1sur 6

/*Write a program to remove extra spaces from string and

count number of words in it. */

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

void main()
{
clrscr();
char str[20];
int w=1;
cout<<"enter the string \n";
gets(str);
for(int j=0; str[j]!='\0'; j++)
{
if(str[j]==' ')
{
int k=j;
while(str[k+1]==' ')
{
for(int m=1 ;str[k+m]!='\0';m++)
str[k+m]=str[k+m+1];
}
w++;
}
}
cout<<"the modified string is \n";
puts(str);
cout<<"number of words are"<<w;
getch();
}

HARSHIT GUPTA (XI A7)


/*Write a program to find the length of a string without
using strln() function.*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<stdio.h>

void main()
{
clrscr();
char str[20];
int j=0;
cout<<"enter the string \n";
gets(str);
for(int i=0; str[i]!='\0'; i++)
j++;

cout<<"the length of the string is "<<j;


getch();
}

HARSHIT GUPTA (XI A7)


/*Write a program to check whether a string is pallendrome or not*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<stdio.h>

void main()
{
clrscr();
char str[50]=" ",num[50]=" ";
int j=0,x=0;
cout<<"enter the string \n";
gets(str);
for(int i=0; str[i]!='\0'; i++)
j++;
for(int k=0; k!=j+1; k++)
{
num[k]=str[j-k-1];
}
num[k+1]='\0';
for(i=0; str[i]!='\0';i++)
{
if(str[i]!=num[i])
x=1;
}
if(x==0)
cout<<"\n the string is a pallendrome \n";
else
cout<<"\n the string is not a pallendrome \n";
getch();
}

HARSHIT GUPTA (XI A7)


/*Write a program to concatinate two strings without using
string functions*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<stdio.h>

void main()
{
clrscr();
char str[50],num[50];
cout<<"enter the two strings \n";
gets(str);
gets(num);
for(int x=0; str[x]!='\0'; x++);
str[x]=' ';
x++;
for(int y=0; num[y]!='\0'; y++)
{
str[x]=num[y];
x++;
}
str[x]='\0';
cout<<"the concatinated string is "<<str;
getch();
}

HARSHIT GUPTA (XI A7)


/*Write a program to change the case of each letter in the
string. */
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<stdio.h>

void main()
{
clrscr();
char str[50];
int x,y,z;
cout<<"enter the string \n";
gets(str);
for(x=0;str[x]!='\0';x++)
{
if(str[x]>=65 && str[x]<=90)
str[x]=str[x]+32;
else
{
if(str[x]>=90)
str[x]=str[x]-32;
}
}
cout<<"the new string is "<<str;
getch();
}

HARSHIT GUPTA (XI A7)


ASSIGNMENT 5

STRING PROGRAMS

1. Write a program to remove extra spaces from string


and count number of words in it.
2. Write a program to find the length of a string
without using strln() function.
3. Write a program to check whether a string is
pallendrome or not
4. Write a program to concatinate two strings without
using string functions
5. Write a program to change the case of each letter in
the string.

HARSHIT GUPTA (XI A7)

Vous aimerez peut-être aussi