[1]finding length of a String without using library function strlen
#include <stdio.h>
int main()
{
char str[100],i;
printf("Enter a string: \n");
scanf("%s",str);
// '\0' represents end of String
for(i=0; str[i]!='\0'; ++i);
printf("\nLength of input string: %d",i);
return 0;
}
[2]Program to separate the individual characters from a string
#include <stdio.h>
#include <string.h>
int main()
{
char string[] = "characters";
printf("Individual characters from given string:\n");
//Iterate through the string and display individual character
for(int i = 0; i < strlen(string); i++){
printf("%c ", string[i]);
}
return 0;
}
[3]Write a program in C to print individual characters of string in reverse order
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str[100]; /* Declares a string of size 100 */
int l,i;
printf("Input the string : ");
fgets(str, sizeof str, stdin);
l=strlen(str);
printf("The characters of the string in reverse are : \n");
for(i=l;i>=0;i--)
{
printf("%c ", str[i]);
}
printf("\n");
}
Program to Count Total Number of Words in a String
- #include <stdio.h>
- #include <conio.h>
- void main()
- {
- char a[100];
- int len,i,word=1;
- clrscr();
- printf("\nENTER A STRING: ");
- gets(a);
- len=strlen(a);
- for(i=0;i<len;i++)
- {
- if(a[i]!=' ' && a[i+1]==' ')
- word=word+1;
- }
- printf("\nTHERE ARE %d WORDS IN THE STRING",word);
- getch();
- }
C Program to Compare Two Strings without using strcmp
#include<stdio.h> int main() { char s1[100], s2[100]; int i; printf("\nEnter two strings :"); gets(s1); gets(s2); i = 0; // while s1 is equal to s2 while (s1[i] == s2[i] && s1[i] != '\0') i++; if (s1[i] > s2[i]) printf("s1 is greater than s2"); else if (s1[i] < s2[i]) printf("s1 is less than s2"); else printf("s1 is equal to s2"); return 0; } |
Output:
Enter two strings :ac
acde
s1 is less than s2
Program to Count Alphabets Digits Special characters in a string
#include <stdio.h> #define MAX_SIZE 100 //Maximum size of the string int main() { char string[MAX_SIZE]; int alphabets, digits, others, i; alphabets = digits = others = i = 0; /* Reads a string from user */ printf("\nEnter any string : "); gets(string); /* * Checks each character of string */ while(string[i]!='\0') { if((string[i]>='a' && string[i]<='z') || (string[i]>='A' && string[i]<='Z')) { alphabets++; } else if(string[i]>='0' && string[i]<='9') { digits++; } else { others++; } i++; } printf("\nAlphabets = [ %d ]\n", alphabets); printf("\nDigits = [ %d ]\n", digits); printf("\nSpecial characters = [ %d ]\n", others); return 0; } |
OUTPUT : :
| /* C Program to Count Alphabets Digits Special characters in a string */ Enter any string : Hello www.codezclub.com Alphabets = [ 20 ] Digits = [ 0 ] Special characters = [ 3 ]
Copy String Without Using strcpy()#include <stdio.h>
int main() {
char s1[100], s2[100], i;
printf("Enter string s1: ");
fgets(s1, sizeof(s1), stdin);
for (i = 0; s1[i] != '\0'; ++i) {
s2[i] = s1[i];
}
s2[i] = '\0';
printf("String s2: %s", s2);
return 0;
}
Output Enter string s1: Hey fellow programmer.
String s2: Hey fellow programmer. Write a program in C to count total number of vowel or consonant in a string.
Sample Output: Count total number of vowel or consonant :
----------------------------------------------
Input the string : Welcome to w3resource.com
The total number of vowel in the string is : 9
The total number of consonant in the string is : 12 ***********************------- Program to find maximum and minimum occurring character in a string#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[100];
int i,j;
char Maxrepeatedchar[100];
int count=0;
int maxcount=0;
printf("Enter a string");
gets(str);
for(i=0; i<strlen(str); i++){
count=0;
for(j=0; i<strlen(str) ;j++){
if(str[i]==str[j]){
count++;
}
}
if(maxcount<count){
maxcount=count;
maxrepeatedchar=str[i];
}
}
printf("The char %c has been repeated maximum of %d times",maxrepeatedchar,maxcount);
return 0;
}
************************** C Programming: Sort a string array in ascending order#include <stdio.h>
#include <string.h>
void main()
{
char str[100],ch;
int i,j,l;
printf("Input the string : ");
fgets(str, sizeof str, stdin);
l=strlen(str);
for(i=1;i<l;i++)
for(j=0;j<l-i;j++)
if(str[j]>str[j+1])
{
ch=str[j];
str[j] = str[j+1];
str[j+1]=ch;
}
printf("After sorting the string appears like : \n");
printf("%s\n\n",str);
} **************************************++++++++++++++---------- C Programming: Extract a substring from a given string#include <stdio.h>
void main()
{
char str[100], sstr[100];
int pos, l, c = 0; printf("Input the string : ");
fgets(str, sizeof str, stdin);
printf("Input the position to start extraction :");
scanf("%d", &pos);
printf("Input the length of substring :");
scanf("%d", &l);
while (c < l)
{
sstr[c] = str[pos+c-1];
c++;
}
sstr[c] = '\0';
printf("The substring retrieve from the string is : \" %s\ "\n\n", sstr);
} aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa****************************************************************
Check whether a given substring is present in the given string
#include<stdio.h> #include<string.h>
int main()
{
char mainstring[40]="i love you vikram";
char substring[30]="Pihu";
char *result;
result = strstr(mainstring ,substring);
if(result){
printf("present");}
else {
printf("not Present");
}
Replace lowercase characters by uppercase and vice-versa#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
int i;
printf("\nEnter any string :: ");
gets(str);
printf("\nThe input String is :: [ %s ]\n",str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
else if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
else;
}
printf("\nThe Converted String is :: [ %s ]\n",str);
return 0;
}To count number of times a letter repeated in sentence
#include<stdio.h>
#include<string.h>
main()
{
int i,count=0;
char c,str[100];
printf("Enter a sentence\n");
gets(str);
printf("Enter a character to know it's repetance in sentence\n");
scanf("%c",&c);
for(i=0;i<strlen(str);i++)
{
if(str[i]==c)
{
count++;
}
}
printf("Letter %c repeated %d times\n",c,count);
}Program to remove all characters in a string except alphabets#include<stdio.h>
int main()
{
char str[150];
int i, j;
printf(“\nEnter a string : “);
gets(str);
for(i = 0; str[i] != ‘\0’; ++i)
{
while (!( (str[i] >= ‘a’ && str[i] <= ‘z’) || (str[i] >= ‘A’ && str[i] <= ‘Z’) || str[i] == ‘\0’) )
{
for(j = i; str[j] != ‘\0’; ++j)
{
str[j] = str[j+1];
}
str[j] = ‘\0’;
}
}
printf(“\nResultant String : “);
puts(str);
return 0;
} Find the Frequency of a Character#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter a character to find its frequency: ");
scanf("%c", &ch);
for (int i = 0; str[i] != '\0'; ++i) {
if (ch == str[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
return 0;
}
Output Enter a string: This website is awesome.
Enter a character to find its frequency: e
Frequency of e = 4 Concatenate Two Strings Manually#include<stdio.h>
void main(void)
{
char str1[25],str2[25];
int i=0,j=0;
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
}
Find the largest and smallest word in a string Convert a string to uppercase#include<stdio.h>
#include<string.h>
int main(){
char str[25];
int i;
printf("Enter the string:");
scanf("%s",str);
for(i=0;i<=strlen(str);i++){
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("\nUpper Case String is: %s",str);
return 0;
}
Convert a string to lowercase#include<stdio.h>
#include<string.h>
int main(){
/* This array can hold a string of upto 25
* chars, if you are going to enter larger string
* then increase the array size accordingly
*/
char str[25];
int i;
printf("Enter the string: ");
scanf("%s",str);
for(i=0;i<=strlen(str);i++){
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nLower Case String is: %s",str);
return 0;
}
Check whether a given character is upper case, lower case, number or special character
#include
int main()
{
//Fill the code
char ch;
scanf(“%c”,&ch);
if(ch >= 65 && ch <= 90)
printf(“Upper”);
else if(ch >= 97 && ch <= 122)
printf(“Lower”);
else if(ch >= 48 && ch <= 57)
printf(“Number”);
else
printf(“Symbol”);
return 0;
}
|
No comments:
Post a Comment