Archive for the ‘Strings’ Category

/* Program to replace a substring with another */

#include<stdio.h>
#include<string.h>

void replaceSubstring(char [],char[],char[]);

main()
{
char string[100],sub[100],new[100];
printf(“\nEnter a string: “);
gets(string);
printf(“\nEnter the substring: “);
gets(sub);
printf(“\nEnter the new substring: “);
gets(new);
replaceSubstring(string,sub,new);
printf(“\nThe string after replacing…….\n\n%s\n\n”,string);
}

void replaceSubstring(char string[],char sub[],char new[])
{
int stringLen,subLen,newLen;
int i=0,j,k;
int flag=0,start,end;
stringLen=strlen(string);
subLen=strlen(sub);
newLen=strlen(new);

for(i=0;i<stringLen;i++)
{
flag=0;
start=i;
for(j=0;string[i]==sub[j];j++,i++) /* Checks for the substring */
if(j==subLen-1)
flag=1; /* flag sets when substring is identified */
end=i;
if(flag==0)
i-=j;
else
{
for(j=start;j<end;j++) /* Delete the substring */
{
for(k=start;k<stringLen;k++)
string[k]=string[k+1];
stringLen–;
i–;
}

for(j=start;j<start+newLen;j++)    /* Inserting new substring */
{
for(k=stringLen;k>=j;k–)
string[k+1]=string[k];
string[j]=new[j-start];
stringLen++;
i++;
}
}
}
}

Advertisements

/* Program to check whether the given string is palindrome or not */

#include<stdio.h>
#include<string.h>

int palindrome(char[]);

main()
{
char string[100];
int flag;
printf(“\nEnter a strng: “);
gets(string);
flag=pallindrome(string);
if(flag==0)
printf(“\nThe string is palindrome!!\n”);
else
printf(“\nThe string is not palindrome!!\n”);
}

int palindrome(char string[])
{
int len,i;
len=strlen(string);
for(i=0;i<len/2;i++)
if(string[i]!=string[len-i-1])
return 1;
return 0;
}

Reverse a String

Posted: March 14, 2010 in Strings
Tags:

/* Program to reverse a string */

#include<stdio.h>
#include<string.h>

void reverse(char[]);
main()
{
char string[50];
printf(“\nEnter a string………\n”);
gets(string);
reverse(string);
printf(“\nThe reversed string is…….\n%s\n”,string);
}

void reverse(char string[])
{
int len,i;
char temp;
len=strlen(string);
for(i=0;i<len/2;i++)
{
temp=string[i];
string[i]=string[len-i-1];
string[len-i-1]=temp;
}
}

/* Program to replace a word with another */

#include<stdio.h>
#include<string.h>

void replaceWord(char[],char[],char[]);
main()
{
char sent[100],word[50],key[50];
printf(“\nEnter the string: “);
gets(sent);
printf(“\nEnter the word to be replaced: “);
scanf(“%s”,word);
printf(“\nEnter the new word: “);
scanf(“%s”,key);
replaceWord(sent,word,key);
printf(“\n%s\n”,sent);
}

void replaceWord(char sent[],char word[],char key[])
{
int sentLen,wordLen,keyLen,i,j,k,t;
char temp[50];
sentLen=strlen(sent);
wordLen=strlen(word);
keyLen=strlen(key);

for(i=0;i<sentLen&&sent[i]!=”;i++)
{
for(j=0;sent[i]!=’ ‘&&sent[i]!=”;j++,i++)
{
temp[j]=sent[i]; /* to seperate words */
}

temp[j]=”; /* makes temp a string */

if(strcmp(temp,word)==0)
{
k=i-wordLen;
for(t=0;t<wordLen;t++) /* delete word */
{
for(j=k;j<sentLen;j++)
sent[j]=sent[j+1];
sentLen–;
i–;
}

for(t=0;t<keyLen;t++) /* insert key */
{
for(j=sentLen;j>=k;j–)
sent[j+1]=sent[j];
sent[k]=key[t];
k++;
sentLen++;
i++;
}
}
}
}

Count Number of Words

Posted: February 20, 2010 in Strings
Tags:

/* Program to count the no. of words in given string */

#include<stdio.h>
#include<string.h>

int countWords(char []);

main()
{
char string[100];
int count;
printf(“\nEnter the string: “);
gets(string);
count=countWords(string);
printf(“\nThe no of words: %d\n”,count);
}

int countWords(char string[])
{
int count=0,len,flag=0,i;
len=strlen(string);
for(i=0;string[i]!=”;i++)
{
if(string[i]!=’ ‘)
flag=1;
if(string[i]==’ ‘)
if(string[i-1]!=’ ‘)
count++;
}
if(flag==1&&string[0]!=’ ‘)
count++;
if(string[len-1]==’ ‘)
count–;
return count;
}