Strings - Progs2 (Memmove)
Strings - Progs2 (Memmove)
#include <stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
char s[20],ch;
gets(s);
char *p=s;
for(;*p;p++)
{
if(!isalpha(*p))
{
memmove(p,p+1,strlen(p+1)+1);
p--;
}
}
printf("updated str\n%s",s);
}
====***====
//C program to remove all vowels (both upper case and lowercase) from a
supplied string
#include <stdio.h>
#include<string.h>
#include<ctype.h>
int isvowel(char ch)
{
if(ch>='A' && ch<='Z')
{
ch=ch^32;
}
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
return 1;
}
else
{
return 0;
}
}
int main()
{
char s[20],ch;
gets(s);
char *p=s;
for(;*p;p++)
{
if(isvowel(*p))
{
memmove(p,p+1,strlen(p+1)+1);
p--;
}
}
printf("updated str\n%s",s);
}
====***====
//"Removing All Occurrences of a Character from a String in C"
#include <stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
char s[20],ch;
gets(s);
printf("Enter character");
ch=getchar();
char *p=s;
while(p=strchr(p,ch))
{
memmove(p,p+1,strlen(p+1)+1);
}
printf("updated str\n%s",s);
}
====***====
Write a C program to verify whether two given strings are anagrams of each
other.
#include<stdio.h>
#include<string.h>
void check_anagram(char *s1,char *s2)
{
char *p=s2;
for(;*s1;s1++)
{
s2=p;
for(;*s2;s2++)
{
if(*s1>=65 && *s1<=90)
{
*s1=*s1^32;
}
if(*s2>=65 && *s2<=90)
{
*s2=*s2^32;
}
if(*s1==*s2)
{
memmove(s2,s2+1,strlen(s2+1)+1);
break;
}
}
}
}
int main()
{
char s1[20],s2[20];
//to remove new line s1
fgets(s1,20,stdin);
if(s1[strlen(s1)-1]=='\n')
s1[strlen(s1)-1]='\0';
//to remove new line in s2
fgets(s2,20,stdin);
if(s2[strlen(s2)-1])
s2[strlen(s2)-1]='\0';
int l1=strlen(s1),l2=strlen(s2);
if(l1!=l2)
{
printf("not an Anagram strings");
return 0;
}
check_anagram(s1,s2);
int len=strlen(s2);
if(len==0)
{
printf("Anagram strings");
}
else
{
printf("Not an anagram strings");
}
}
====***====
//"Removing All Occurrences of a given sub string from a String in C"
#include <stdio.h>
#include<string.h>
int main()
{
char s1[30],s2[20],ch;
printf("Enter str1\n");
gets(s1);
printf("Enter str2\n");
gets(s2);
int len=strlen(s2);
char *p=s1;
while(p=strstr(p,s2))
{
memmove(p,p+len,strlen(p+len)+1);
}
printf("updated str\n%s",s1);
}
====***====
//Write a program to reverse all occurrences of sub string in main string
#include <stdio.h>
#include<string.h>
void my_strrev(char *p,int len)
{
int i=0,j=len-1;
char t;
//reverse characters...swap
for(;i<j;i++,j--)
{
t=p[i];
p[i]=p[j];
p[j]=t;
}
}
int main()
{
char s1[30],s2[20],ch;
printf("Enter str1\n");
gets(s1);
printf("Enter str2\n");
gets(s2);
int len=strlen(s2);
char *p=s1;
while(p=strstr(p,s2))
{
my_strrev(p,len);
}
printf("updated str\n%s",s1);
}
====***====
//Write a program to replace all occurrences of sub string in main string with
opposite case of every alphabet.
#include <stdio.h>
#include<string.h>
void case_conversion(char *p,int len)
{
int i;
for(i=0;i<len;i++)
{
p[i]=p[i]^32;
}
}
int main()
{
char s1[30],s2[20],ch;
printf("Enter str1\n");
gets(s1);
printf("Enter str2\n");
gets(s2);
int len=strlen(s2);
char *p=s1;
while(p=strstr(p,s2))
{
case_conversion(p,len);
}
printf("updated str\n%s",s1);
}
====***====
//C Program to Replace Sub strings with asterisks(‘*’) in a String
#include <stdio.h>
#include<string.h>
void Replace_astric(char *p,int len)
{
int i;
for(i=0;i<len;i++)
{
p[i]='*';
}
}
int main()
{
char s1[30],s2[20],ch;
printf("Enter str1\n");
gets(s1);
printf("Enter str2\n");
gets(s2);
int len=strlen(s2);
char *p=s1;
while(p=strstr(p,s2))
{
Replace_astric(p,len);
//memset(p,'*',len);
}
printf("updated str\n%s",s1);
}
====***====
//C program that removes extra spaces from a given string, leaving only single
spaces between words
#include<stdio.h>
#include<string.h>
void remove_spaces_begining(char *p);
void remove_spaces_middle(char *p);
void remove_spaces_begining(char *p)
{
while(*p)
{
if(*p==' ')
{
memmove(p,p+1,strlen(p+1)+1);
--p;
}
else
{
break;
}
p++;
}
}
void remove_spaces_middle(char *p)
{
while(*p)
{
if(*p==' ' && *(p+1)==' ')
{
memmove(p,p+1,strlen(p+1)+1);
p--;
}
p++;
}
}
int main()
{
char s[50];
printf("Enter str1\n");
gets(s);
remove_spaces_begining(s);
remove_spaces_middle(s);
if(s[strlen(s)-1]==' ')
s[strlen(s)-1]='\0';
printf("updated str\n%s",s);
}