Category Archives: String

Permutation of String in C and C++

Here you will get program for permutation of string in C and C++. Permutation means all possible arrangements of given set of numbers or characters. For a string with n characters can have total n! arrangements. Take below example. Here we are using backtracking method to find the permutation of a string. Watch below video… Read More »

C/C++ Program to Remove Spaces From String

For example given string is “the crazy programmer”. Now we have to remove all the spaces present in the string.   Before: the crazy programmer After: thecrazyprogrammer Below I have shared a program that perform above task with simple approach. You can ask your queries in the comment section. C/C++ Program to Remove Spaces From… Read More »

Anagram in C

Here is the program for anagram in c.   Two strings are said to be anagrams if by rearranging the characters of any one string will make both strings equal.   Example: “adssfa” and “dsasfa” are anagrams “adsfa” and “sdfac” are not anagrams How to Check two Strings are Anagrams or not? So what we will… Read More »

C/C++ Program to Find Substring in String (Pattern Matching)

Pattern matching refers to find the position where a string pattern appears in a given string. If the requird string is not present in given text, then it returns the value zero. If the required string is present in a given string, it returns the position of occurrence of required string or substring. Also Read: C… Read More »

C program to concatenate two strings without using strcat() function

#include<stdio.h> #include<conio.h> void main() { char s1[30],s2[30],s3[60]; int i,j; clrscr(); printf(“Enter first string:”); gets(s1); printf(“Enter second string:”); gets(s2); for(i=0;s1[i]!=’’;++i) s3[i]=s1[i]; for(j=0;s2[j]!=’’;++j) s3[i+j]=s2[j]; s3[i+j]=’’; printf(“nThe concatenated string is: %s”,s3); getch(); }

C program which reads a text and count all occurrences of a particular word

#include<stdio.h> #include<conio.h> #include<string.h> void main() { int i=0,j=0,count=0; char str1[100],str2[20],str3[20]; clrscr(); printf(“Enter the text: “); gets(str1); printf(“Enter word to count: “); gets(str2); while(str1[i]!=”) { while(str1[i]!=’ ‘&&str1[i]!=”) //copying the word from the text to a new string str3[j++]=str1[i++]; str3[j]=”; //assigning null character at the end of string j=0; if((strcmpi(str2,str3))==0) //comparing the given word with the copied… Read More »

C program to count number of words in a string

#include<stdio.h> #include<conio.h> void main() { int i,words=1; char str[100]; clrscr(); printf(“Enter a string:”); gets(str); for(i=0;str[i]!=’’;++i) if(str[i]==’ ‘) words++; printf(“No. of words are %d”,words); getch(); }

C program to check whether given string is palindrome or not

#include<stdio.h> #include<conio.h> #include<string.h> void main() { int i,j,flag=1,len; char str[50]; clrscr(); printf(“Enter any string:”); gets(str); len=strlen(str); for(i=0,j=len-1;i<len/2;++i,–j) if(str[i]!=str[j]) { flag=0; break; } if(flag) printf(“String is palindrome“); else printf(“String is not palindrome”); getch(); }

C program to reverse a string

#include<stdio.h> #include<conio.h> #include<string.h> void main() { int i,n; char a[30]; clrscr(); printf(“Enter any string:”); gets(a); n=strlen(a); printf(“Reverse of string:”); for(i=(n-1);i>=0;–i) printf(“%c”,a[i]); getch(); }

C program to find length of a string

#include<stdio.h> #include<conio.h> void main() { int i; char str[50]; clrscr(); printf(“Enter a string:”); gets(str); for(i=0;str[i]!=’’;++i); printf(“Lenth of string is %d”,i); getch(); }

C program to read a string and print it in alphabetical order

#include<stdio.h> #include<conio.h> #include<string.h> void main() { int i,j,n,ch1,ch2; char a[50],temp; clrscr(); printf(“Enter any string:”); scanf(“%s”,a); n=strlen(a); for(i=1;i<n;++i) for(j=0;j<(n-i);++j) { ch1=a[j]; ch2=a[j+1]; if(ch1>ch2) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } printf(“String after arranging %s”,a); getch(); }