//C code for searchin a string
//1.strStr: c code for user defined function to find a string from multiple strings
#include<stdio.h>
int compare_string(char*, char*);
int main()
{
char first[100], second[100], result;
printf("Enter first string\n");
gets(first);
printf("Enter second string\n");
gets(second);
result = compare_string(first, second);
if ( result == 0 )
printf("Both strings are same.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}
int compare_string(char *first, char *second)
{
while(*first==*second)
{
if ( *first == '\0' || *second == '\0' )
break;
first++;
second++;
}
if( *first == '\0' && *second == '\0' )
return 0;
else
return -1;
}
/*
OUTPUT:
-bash-3.2$ vi strStr.c
-bash-3.2$ gcc strStr.c
/tmp/ccUUzk8E.o: In function `main':
strStr.c:(.text+0x27): warning: the `gets' function is dangerous and should not be used.
-bash-3.2$ ./a.out
Enter few strings:
all is well
Enter the string to search:
code
NOT Present
-bash-3.2$ ./a.out
Enter few strings:
all is well
Enter the string to search:
all
Present at location :1
-bash-3.2$ ./a.out
Enter few strings:
all is well
Enter the string to search:
is
Present at location :5
-bash-3.2$ ./a.out
Enter few strings:
all is well
Enter the string to search:
well
Present at location :8
-bash-3.2$ ./a.out
Enter few strings:
all is well
Enter the string to search:
ll
Present at location :2
-bash-3.2$
*/
C code for comparing two strings
//1.strStr: c code for user defined function to find a string from multiple strings
#include<stdio.h>
int compare_string(char*, char*);
int main()
{
char first[100], second[100], result;
printf("Enter first string\n");
gets(first);
printf("Enter second string\n");
gets(second);
result = compare_string(first, second);
if ( result == 0 )
printf("Both strings are same.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}
int compare_string(char *first, char *second)
{
while(*first==*second)
{
if ( *first == '\0' || *second == '\0' )
break;
first++;
second++;
}
if( *first == '\0' && *second == '\0' )
return 0;
else
return -1;
}
/*
OUTPUT:
-bash-3.2$ vi strStr.c
-bash-3.2$ gcc strStr.c
/tmp/ccUUzk8E.o: In function `main':
strStr.c:(.text+0x27): warning: the `gets' function is dangerous and should not be used.
-bash-3.2$ ./a.out
Enter few strings:
all is well
Enter the string to search:
code
NOT Present
-bash-3.2$ ./a.out
Enter few strings:
all is well
Enter the string to search:
all
Present at location :1
-bash-3.2$ ./a.out
Enter few strings:
all is well
Enter the string to search:
is
Present at location :5
-bash-3.2$ ./a.out
Enter few strings:
all is well
Enter the string to search:
well
Present at location :8
-bash-3.2$ ./a.out
Enter few strings:
all is well
Enter the string to search:
ll
Present at location :2
-bash-3.2$
*/
C code for comparing two strings
//2.
strCmp: c code for user defined function to compare
two strings
//finding a string in strings array
#include<stdio.h>
int strStr(char *str1, char *str2);
int main()
{
char str1[100],str2[10];
printf("Enter few strings:\n");
gets(str1);
printf("Enter the string to search:\n");
gets(str2);
int result;
result=strStr(str1,str2);
if(result==0)
printf("Equals\n");
else
printf("NOT Equals\n");
//return 0;
}
int strStr(char *str1, char *str2)
{
while(*str1==*str2)
{
if(*str1=='\0' || *str2=='\0')
break;
str1++;
str2++;
}
if(*str1=='\0' && *str2=='\0')
return 0;
else
return -1;
}
/*
OUTPUT:
-bash-3.2$ gcc strCmp.c
/tmp/ccyAmuWc.o: In function `main':
strCmp.c:(.text+0x27): warning: the `gets' function is dangerous and should not be used.
-bash-3.2$ ./a.out
Enter few strings:
india
Enter the string to search:
india
Equals
-bash-3.2$ ./a.out
Enter few strings:
india
Enter the string to search:
in
NOT Equals
-bash-3.2$ ./a.out
Enter few strings:
in
Enter the string to search:
india
NOT Equals
-bash-3.2$ india
-bash: india: command not found
-bash-3.2$ ./a.out
Enter few strings:
India
Enter the string to search:
india
NOT Equals
-bash-3.2$
*/
C code for finding string length//3. strLen:c code for user defined function to find length of a string
#include<stdio.h>
int main()
{
char arr[20];
printf("Enter a string:\n");
gets(arr);
int result;
result=strLen(arr);
printf("Length of the string is:%d\n",result);
return 0;
}
int strLen(char *str)
{
char *tmp;
tmp=str;
while(*tmp)
{
tmp++;
}
return (tmp-str);
}
/*
-bash-3.2$ ./a.out
Enter a string:
India
Length of the string is:5
-bash-3.2$ ./a.out
Enter a string:
India is great
Length of the string is:14
*/
C code for copying string//4. strCpy:C code for user defined function to copy one string to another
#include<stdio.h>
void strCpy(char *p2, char *p1);
int main()
{
char str1[20];
char str2[20];
printf("Enter a string:\n");
gets(str1);
strCpy(str2,str1);
printf("Copied string is:%s\n",str1);
return 0;
}
void strCpy(char *p2, char *p1)
{
while(*p1!='\0')
{
*p2=*p1;
p2++;
p1++;
}
*p2='\0';
}
/*
OUTPUT:
-bash-3.2$ gcc strCpy.c
/tmp/ccaP29bY.o: In function `main':
strCpy.c:(.text+0x24): warning: the `gets' function is dangerous and should not be used.
-bash-3.2$ ./a.out
Enter a string:
india
Copied string is:india
-bash-3.2$ ./a.out
Enter a string:
hello india
Copied string is:hello india
-bash-3.2$
*/
C code for reversing a string
//5. strRev: C code for user defined function to reverse a string
//strRev"User defined function to reverse a string
#include<stdio.h>
#include<string.h>
void strRev(char *arr1);
int main()
{
char arr1[20];
printf("Enter a string to be reversed:\n");
gets(arr1);
strRev(arr1);
printf("Reversed string is :%s\n",arr1);
return 0;
}
void strRev(char *arr1)
{
char *begin;
char *end;
char temp;
begin = arr1;
end = arr1;
int length, i;
length=strlen(arr1);
for ( i = 0 ; i < (length) - 1 ; i++ )
end++;
for ( i = 0 ; i < length/2 ; i++ )
{
temp = *end;
*end = *begin;
*begin = temp;
begin++;
end--;
}
}
/*OUTPUT:
-bash-3.2$ ./a.out
Enter a string to be reversed:
hello
Reversed string is :olleh
-bash-3.2$ ./a.out
Enter a string to be reversed:
hello india
Reversed string is :aidni olleh
-bash-3.2$ ./a.out
Enter a string to be reversed:
a
Reversed string is :a
-bash-3.2$
*/
//finding a string in strings array
#include<stdio.h>
int strStr(char *str1, char *str2);
int main()
{
char str1[100],str2[10];
printf("Enter few strings:\n");
gets(str1);
printf("Enter the string to search:\n");
gets(str2);
int result;
result=strStr(str1,str2);
if(result==0)
printf("Equals\n");
else
printf("NOT Equals\n");
//return 0;
}
int strStr(char *str1, char *str2)
{
while(*str1==*str2)
{
if(*str1=='\0' || *str2=='\0')
break;
str1++;
str2++;
}
if(*str1=='\0' && *str2=='\0')
return 0;
else
return -1;
}
/*
OUTPUT:
-bash-3.2$ gcc strCmp.c
/tmp/ccyAmuWc.o: In function `main':
strCmp.c:(.text+0x27): warning: the `gets' function is dangerous and should not be used.
-bash-3.2$ ./a.out
Enter few strings:
india
Enter the string to search:
india
Equals
-bash-3.2$ ./a.out
Enter few strings:
india
Enter the string to search:
in
NOT Equals
-bash-3.2$ ./a.out
Enter few strings:
in
Enter the string to search:
india
NOT Equals
-bash-3.2$ india
-bash: india: command not found
-bash-3.2$ ./a.out
Enter few strings:
India
Enter the string to search:
india
NOT Equals
-bash-3.2$
*/
C code for finding string length//3. strLen:c code for user defined function to find length of a string
#include<stdio.h>
int main()
{
char arr[20];
printf("Enter a string:\n");
gets(arr);
int result;
result=strLen(arr);
printf("Length of the string is:%d\n",result);
return 0;
}
int strLen(char *str)
{
char *tmp;
tmp=str;
while(*tmp)
{
tmp++;
}
return (tmp-str);
}
/*
-bash-3.2$ ./a.out
Enter a string:
India
Length of the string is:5
-bash-3.2$ ./a.out
Enter a string:
India is great
Length of the string is:14
*/
C code for copying string//4. strCpy:C code for user defined function to copy one string to another
#include<stdio.h>
void strCpy(char *p2, char *p1);
int main()
{
char str1[20];
char str2[20];
printf("Enter a string:\n");
gets(str1);
strCpy(str2,str1);
printf("Copied string is:%s\n",str1);
return 0;
}
void strCpy(char *p2, char *p1)
{
while(*p1!='\0')
{
*p2=*p1;
p2++;
p1++;
}
*p2='\0';
}
/*
OUTPUT:
-bash-3.2$ gcc strCpy.c
/tmp/ccaP29bY.o: In function `main':
strCpy.c:(.text+0x24): warning: the `gets' function is dangerous and should not be used.
-bash-3.2$ ./a.out
Enter a string:
india
Copied string is:india
-bash-3.2$ ./a.out
Enter a string:
hello india
Copied string is:hello india
-bash-3.2$
*/
C code for reversing a string
//5. strRev: C code for user defined function to reverse a string
//strRev"User defined function to reverse a string
#include<stdio.h>
#include<string.h>
void strRev(char *arr1);
int main()
{
char arr1[20];
printf("Enter a string to be reversed:\n");
gets(arr1);
strRev(arr1);
printf("Reversed string is :%s\n",arr1);
return 0;
}
void strRev(char *arr1)
{
char *begin;
char *end;
char temp;
begin = arr1;
end = arr1;
int length, i;
length=strlen(arr1);
for ( i = 0 ; i < (length) - 1 ; i++ )
end++;
for ( i = 0 ; i < length/2 ; i++ )
{
temp = *end;
*end = *begin;
*begin = temp;
begin++;
end--;
}
}
/*OUTPUT:
-bash-3.2$ ./a.out
Enter a string to be reversed:
hello
Reversed string is :olleh
-bash-3.2$ ./a.out
Enter a string to be reversed:
hello india
Reversed string is :aidni olleh
-bash-3.2$ ./a.out
Enter a string to be reversed:
a
Reversed string is :a
-bash-3.2$
*/
C
code for concatenating strings//6.
strCat: C code for user defined string concatenation of two
strings
#include<stdio.h>
void strCat(char * str1, char *str2);
int main()
{
char str1[20];
char str2[20];
printf("Enter first string:\n");
gets(str1);
printf("Enter second string to concatenate to first:\n");
gets(str2);
strCat(str1,str2);
printf("Conacatenated String is:%s\n",str1);
return 0;
}
void strCat(char *p11, char *p22)
{
char *p1, *p2;
p1=p11;
p2=p22;
while(*p1)
p1++;
while(*p2)
{
*p1=*p2;
p2++;
p1++;
}
*p1='\0';
}
/*
Enter first string:
Hello India
Enter second string to concatenate to first:
How are you
Conacatenated String is:Hello IndiaHow are you
#include<stdio.h>
void strCat(char * str1, char *str2);
int main()
{
char str1[20];
char str2[20];
printf("Enter first string:\n");
gets(str1);
printf("Enter second string to concatenate to first:\n");
gets(str2);
strCat(str1,str2);
printf("Conacatenated String is:%s\n",str1);
return 0;
}
void strCat(char *p11, char *p22)
{
char *p1, *p2;
p1=p11;
p2=p22;
while(*p1)
p1++;
while(*p2)
{
*p1=*p2;
p2++;
p1++;
}
*p1='\0';
}
/*
Enter first string:
Hello India
Enter second string to concatenate to first:
How are you
Conacatenated String is:Hello IndiaHow are you
*/
===================================
===================================
C code for searching a word
and its location
//1a. strstr:String search
return location
#include<stdio.h>
int strStrLocation(char *str1,
char *str2);
int main()
{
char str1[20];
char str2[20];
printf("Enter
String1:\n");
gets(str1);
printf("Enter
String2:\n");
gets(str2);
int r=0;
r=strStrLocation(str1,str2);
if(r!=-1)printf("found at
%d\n",r+1);
else printf("Not
found\n");
return 0;
}
int strStrLocation(char
*str11, char *str22)
{
char *str1;
char *str2;
str1=str11;
str2=str22;
int location=0;
while(*str11)
{
while(*str1==*str2)
{
str1++;
str2++;
if(*str1=='\0'||*str2=='\0')
break;
}
if(*str2=='\0')
break;
str11++; location++;
str1=str11;
str2=str22;
}//first while
if(*str11)
{
printf("%c\n",*str11);
return location;
}
else
return -1;
}
/*
Enter String1:
india
Enter String2:
ind
i
found at 1
-bash-3.2$ ./a.out
Enter String1:
ind
Enter String2:
india
Not found
-bash-3.2$ ./a.out
Enter String1:
india is grt
Enter String2:
grt
g
found at 10
*/
C code for string compare
returning -1 0 1
//2a. strcmp: string
compare -1,0,1
#include<stdio.h>
int strcompare(char *str1,
char *str2);
int main()
{
char str1[20];
char str2[20];
printf("Enter
String1:\n");
gets(str1);
printf("Enter
String1:\n");
gets(str2);
int r;
r=strcompare(str1,str2);
if(r==0)printf("Equal\n");
else if(r>0)printf("Not
Equal:%s comes after %s\n",str1,str2);
else printf("Not Equal:%s
comes after %s\n",str2,str1);
return 0;
}
int strcompare(char *str1,
char *str2)
{
while(((*str1-*str2)==0) &&
( (*str1!='\0')|| (*str2!='\0')))
{
str1++;
str2++;
}
printf("*str1=%d,*str2=%d\n",*str1,*str2);
return(*str1-*str2);
}
/*
-bash-3.2$ ./a.out
Enter String1:
ind
Enter String1:
ind
Equal
-bash-3.2$ ./a.out
Enter String1:
ind
Enter String1:
india
Not Equal:india comes after
ind
-bash-3.2$ ./a.out
Enter String1:
india
Enter String1:
ind
Not Equal:india comes after
ind
-bash-3.2$ ./a.out
Enter String1:
india
Enter String1:
media
Not Equal:media comes after
india
-bash-3.2$ ./a.out
Enter String1:
234
Enter String1:
123
Not Equal:234 comes after 123
-bash-3.2$ ./a.out
Enter String1:
india
Enter String1:
India
Not Equal:india comes after
India
*/
C code for sorting words in array alphabetical order
//3a. sorting words in
array in alphabetical order
#include<stdio.h>
#include<string.h>
int main()
{
char *str[]={
"Gona","Ram","Samy","Banta","Amita"
};
int i,j;
char *t;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
printf("\nstr[i=%d]%s\t",i,str[i]);
printf("str[j=%d]%s\n",j,str[j]);
if( ( strcmp(str[i],str[j])
)>0)
{
t=str[i];
str[i]=str[j];
str[j]=t;
}
}
printf("\nstr[iiiiii=%d]%s\t",i,str[i]);
}
printf("Sorted Names
are:\n");
for(i=0;i<5;i++)
{
printf("%s\n",str[i]);
}
return 0;
}
/*
-bash-3.2$ vi strSort.c
-bash-3.2$ ./a.out
str[i=0]Gona str[j=1]Ram
str[i=0]Gona str[j=2]Samy
str[i=0]Gona str[j=3]Banta
str[i=0]Banta str[j=4]Amita
str[iiiiii=0]Amita
str[i=1]Ram str[j=2]Samy
str[i=1]Ram str[j=3]Gona
str[i=1]Gona str[j=4]Banta
str[iiiiii=1]Banta
str[i=2]Samy str[j=3]Ram
str[i=2]Ram str[j=4]Gona
str[iiiiii=2]Gona
str[i=3]Samy str[j=4]Ram
str[iiiiii=3]Ram
str[iiiiii=4]Samy Sorted Names
are:
Amita
Banta
Gona
Ram
Samy
*/
C code for sorting words in array like dictionary
//4a. sorting words in
array without library function
#include<stdio.h>
#include<string.h>
int main()
{
//below str is static array of
char ptrs so must be initialized
char *str[]={
"san","sa",
"santa","santa",
"Amita","Amit",
"zan","xow"
};
int i,j;
char *t;
for(i=0;i<8;i++)
{
for(j=i+1;j<8;j++)
{
printf("\nstr[i=%d]%s\t",i,str[i]);
printf("str[j=%d]%s\n",j,str[j]);
if( ( strCmp(str[i],str[j])
)>0)
{
t=str[i];
str[i]=str[j];
str[j]=t;
}
}
printf("\nstr[iiiiii=%d]%s\t",i,str[i]);
}
printf("Sorted Names
are:\n");
for(i=0;i<8;i++)
{
printf("%s\n",str[i]);
}
return 0;
}
int strCmp(char *s1,char *s2)
{
//if(*s1!='\0'&&*s2!='\0')
// 2
//{
while( *s1-*s2==0 &&
(*s1!='\0' || *s2!='\0')) // 3
{
s1++;
s2++;
}
//}
return(*s1-*s2); // 4
}
/*
-bash-3.2$ ./a.out
str[i=0]san str[j=1]sa
str[i=0]sa str[j=2]santa
str[i=0]sa str[j=3]santa
str[i=0]sa str[j=4]Amita
str[i=0]Amita str[j=5]Amit
str[i=0]Amit str[j=6]zan
str[i=0]Amit str[j=7]xow
str[iiiiii=0]Amit
str[i=1]san str[j=2]santa
str[i=1]san str[j=3]santa
str[i=1]san str[j=4]sa
str[i=1]sa str[j=5]Amita
str[i=1]Amita str[j=6]zan
str[i=1]Amita str[j=7]xow
str[iiiiii=1]Amita
str[i=2]santa str[j=3]santa
str[i=2]santa str[j=4]san
str[i=2]san str[j=5]sa
str[i=2]sa str[j=6]zan
str[i=2]sa str[j=7]xow
str[iiiiii=2]sa
str[i=3]santa str[j=4]santa
str[i=3]santa str[j=5]san
str[i=3]san str[j=6]zan
str[i=3]san str[j=7]xow
str[iiiiii=3]san
str[i=4]santa str[j=5]santa
str[i=4]santa str[j=6]zan
str[i=4]santa str[j=7]xow
str[iiiiii=4]santa
str[i=5]santa str[j=6]zan
str[i=5]santa str[j=7]xow
str[iiiiii=5]santa
str[i=6]zan str[j=7]xow
str[iiiiii=6]xow
str[iiiiii=7]zan Sorted Names
are:
Amit
Amita
sa
san
santa
santa
xow
zan
*/
C code for deleting special
characters from a paragraph
//5a. counting number of
chars, digits and special character
#include<stdio.h>
int main()
{
int i;
int
character=0,digit=0,splchar=0;
char strr[20];
printf("Enter a
string:\n");
gets(strr);
char *str=strr;//must
while(*str!='\0')
//for(i=0;i<strlen(strr);i++)
{
printf("%c\n",*str);
if(
( (*str>=65)&&(*str<=91
))||
( (*str>=97)&&(*str<=123))
)
character++;
else
if((*str>=48)&&(*str<=57))
digit++;
else
splchar++;
str++;
}
printf("Number of
chars=%d\n",character);
printf("Number of
digits=%d\n",digit);
printf("Number of
splchar=%d\n",splchar);
printf("\n");
return 0;
}
/*
imanoj123!@#$$%^&*
m
a
n
o
j
1
2
3
!
@
#
$
$
%
^
&
*
Number of chars=5
Number of digits=3
Number of splchar=9
*/
C code for counting vowels and consonants
//6a. counting number of
vowels and consonants
#include<stdio.h>
int main()
{
int i;
int vowel=0,conso=0;
char strr[20];
printf("Enter a
string:\n");
gets(strr);
char *str=strr;//must
while(*str!='\0')
//for(i=0;i<strlen(strr);i++)
{
printf("%c\n",*str);
if( (*str=='a')||
(*str=='A')||
(*str=='e')||
(*str=='E')||
(*str=='i')||
(*str=='I')||
(*str=='o')||
(*str=='O')||
(*str=='u')||
(*str=='U'))
//printf("Its vowel");
vowel++;
else
conso++;
str++;
}
printf("Number of
vowels=%d\n",vowel);
printf("Number of
consonents%d\n",conso);
printf("\n");
return 0;
}
/*
Enter a string:
India
I
n
d
i
a
Number of vowels=3
Number of consonents2
*/
C code for converting Upper to Lower and vice versa
//7. Converting lower to
upper and vice versa without
using library function
/*
Easy program just taking care
of the Ascii values and and there case.
The differnce between
Uppercase character and Lowercase character is 32.
So if the character is in
Uppercase we add 32 to change it to Lowercase and similarly we
substract 32 from Lowercase character to make it Uppercase character.
*/
#include<stdio.h>
int main()
{
char string[30];
printf("Eneter a
string:\n");
gets(string);
printf("u entered:
%s\n",string);
char *str;
str=string;
while(*str!='\0')
{
if((*str>=65) &&
(*str<=91))
{
*str=*str+32;
str++;
}
else
if((*str>=97) &&
(*str<=123))
{
*str=*str-32;
str++;
}
else
{
printf("Not a
character.\n");
break;
}
}
printf("Converted string
is: %s\n",string);
return 0;
}
/*
#include<stdio.h>
int main()
{
char str[30];
printf("Eneter a
string:\n");
gets(str);
printf("u entered:
%s\n",str);
int i;
for(i=0;str[i]!='\0';i++)
{
if((str[i]>=65) &&
(str[i]<=91))
{
str[i]=str[i]+32;
}
else
if((str[i]>=97) &&
(str[i]<=123))
{
str[i]=str[i]-32;
}
else
{
printf("Not a
character.\n");
break;
}
}
printf("Converted string
is: %s\n",str);
return 0;
}
*/
C code for reversing a sentence keeping words as it is
C code for reversing a sentence keeping words as it is
//8. C program to reverse a
sentence keeping the words as it is
#include<stdio.h>
void reverse1(char *x);
void reverse2(char *first,
char *last);
int main()
{
char sentence[40];
printf("Enter few
string:\n");
gets(sentence);
printf("You
Entered:%s\n",sentence);
reverse1(sentence);
return 0;
}
void reverse1(char *s)
{
char *temp=s;
char *word=s;
//temp=s;
//word=s;
while(*temp)
{
temp++;
if(*temp=='\0')//then reverse
the word
{
reverse2(word,temp-1);
}
else if(*temp==' ')//then
reverse the word
{
reverse2(word,temp-1);
word=temp+1;//word after space
becomes the new word
}
}
reverse2(s,temp-1);//then
reverse the sentence
printf("Reversed String
is:%s\n",s);
}
void reverse2(char *firstt,
char *lastt)
{
char t;
while(firstt<lastt)
{
t=*firstt;
*firstt=*lastt;
*lastt=t;
firstt++;
lastt--;
}
}
/*Enter few string:
India is Great
You Entered:India is Great
Reversed String is:Great is
India
*/
C code for reversing sentence
character wise
//9. C program to reverse a
sentence character wise
#include<stdio.h>
void reverse1(char *x);
void reverse2(char *first,
char *last);
int main()
{
char sentence[40];
printf("Enter few
string:\n");
gets(sentence);
printf("You
Entered:%s\n",sentence);
reverse1(sentence);
return 0;
}
void reverse1(char *s)
{
char *temp;
char *word;
temp=s;
word=s;
while(*temp)
{
temp++;
}
reverse2(s,temp-1);//then
reverse the sentence
printf("Reversed String
is:%s\n",s);
}
void reverse2(char *firstt,
char *lastt)
{
char t;
while(firstt<lastt)
{
t=*firstt;
*firstt=*lastt;
*lastt=t;
firstt++;
lastt--;
}
}
/*
Enter few string:
India is Great
You Entered:India is Great
Reversed String is:taerG si
aidnI
*/
C
code for deleting duplicate elements in array
//10. C Program to delete duplicate elements in an array
//C code for removing repeated values of an array
#include<stdio.h>
int main()
{
int a[20],i,j,k,n;
printf("\nnEnter array size : ");
scanf("%d",&n);
printf("\nnAccept Numbers : ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nnOriginal array is : ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
printf("\nnUpdated array is : ");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;)
{
if(a[j]==a[i])
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n--;//else array will contain 0 in deleted duplicate value
}
else
j++;
}
}
for(i=0;i<n;i++)
printf("%d \t",a[i]);
printf("\n");
}
/*
array size : 5
Numbers : 22
33
44
44
22
array is : 22 33 44 44 22
array is : 22 33 44
*/
//10. C Program to delete duplicate elements in an array
//C code for removing repeated values of an array
#include<stdio.h>
int main()
{
int a[20],i,j,k,n;
printf("\nnEnter array size : ");
scanf("%d",&n);
printf("\nnAccept Numbers : ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nnOriginal array is : ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
printf("\nnUpdated array is : ");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;)
{
if(a[j]==a[i])
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n--;//else array will contain 0 in deleted duplicate value
}
else
j++;
}
}
for(i=0;i<n;i++)
printf("%d \t",a[i]);
printf("\n");
}
/*
array size : 5
Numbers : 22
33
44
44
22
array is : 22 33 44 44 22
array is : 22 33 44
*/
C
code for deleting duplicate characters
//11. C Program to delete duplicate characters in a string/sentence
//C code for removing repeated characters of a string/sentence
#include<stdio.h>
#include<string.h>
int main()
{
char a[200];
int i,j,k,n;
printf("Enter a few chars and strings:\n");
gets(a);
printf("\nEntered string is :%s \n",a);
printf("\nUpdated string without repeated characters is\n");
n=strlen(a);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;)//mind it no icrement
{
if(a[j]==a[i])
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n--;//else array will contain 0 in deleted duplicate value
}
else
j++;
}
}
for(i=0;i<n;i++)
printf("%c",a[i]);
printf("\n");
}
/*
Enter a few chars and strings:
india is great aa bb cc dd ee ff gg hh ii jj kk
Entered string is :india is great aa bb cc dd ee ff gg hh ii jj kk
Updated string without repeated characters is
inda sgretbcfhjk
*/
//11. C Program to delete duplicate characters in a string/sentence
//C code for removing repeated characters of a string/sentence
#include<stdio.h>
#include<string.h>
int main()
{
char a[200];
int i,j,k,n;
printf("Enter a few chars and strings:\n");
gets(a);
printf("\nEntered string is :%s \n",a);
printf("\nUpdated string without repeated characters is\n");
n=strlen(a);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;)//mind it no icrement
{
if(a[j]==a[i])
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n--;//else array will contain 0 in deleted duplicate value
}
else
j++;
}
}
for(i=0;i<n;i++)
printf("%c",a[i]);
printf("\n");
}
/*
Enter a few chars and strings:
india is great aa bb cc dd ee ff gg hh ii jj kk
Entered string is :india is great aa bb cc dd ee ff gg hh ii jj kk
Updated string without repeated characters is
inda sgretbcfhjk
*/
C
Code for finding duplicate words
//12. C Program to delete all duplicate words in a sentence
//C code for removing repeated words of among multiple strings
#include <stdio.h>
#include <string.h>
void main()
{
char a[100], b[20][20];
int i, j = 0, k = 0, n, m;
printf("enter a string\n");
scanf("%[^\n]s", a);
for (i = 0;a[i] != '\0';i++)
{
if (a[i] == ' ')
{
b[k][j] = '\0';
k++;
j = 0;
}
else
{
b[k][j] = a[i];
j++;
}
}
b[k][j] = '\0';
for (i = 0;i <= k;i++)
{
for (j = i + 1;j <= k;j++)
{
if (strcmp(b[i], b[j]) == 0)
{
for (m = j;m <= k;m++)
strcpy(b[m], b[m + 1]);
k--;
}
}
}
for (n = 0;n <= k;n++)
{
printf("%s ", b[n]);
}
}
/*
enter a string:
india is great is countrin in india asia great
india is great countrin in asia
*/
//12. C Program to delete all duplicate words in a sentence
//C code for removing repeated words of among multiple strings
#include <stdio.h>
#include <string.h>
void main()
{
char a[100], b[20][20];
int i, j = 0, k = 0, n, m;
printf("enter a string\n");
scanf("%[^\n]s", a);
for (i = 0;a[i] != '\0';i++)
{
if (a[i] == ' ')
{
b[k][j] = '\0';
k++;
j = 0;
}
else
{
b[k][j] = a[i];
j++;
}
}
b[k][j] = '\0';
for (i = 0;i <= k;i++)
{
for (j = i + 1;j <= k;j++)
{
if (strcmp(b[i], b[j]) == 0)
{
for (m = j;m <= k;m++)
strcpy(b[m], b[m + 1]);
k--;
}
}
}
for (n = 0;n <= k;n++)
{
printf("%s ", b[n]);
}
}
/*
enter a string:
india is great is countrin in india asia great
india is great countrin in asia
*/
No comments:
Post a Comment