Problems On String - Compressed
Problems On String - Compressed
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("String: ");
scanf("%[^\n]s",str);
printf("%d\n",strlen(str));
return 0;
}
Problem 2: Write a program to compare two strings.
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str1[100];
char str2[100];
printf("String1: ");
scanf(" %[^\n]s",str1);
printf("String2: ");
scanf(" %[^\n]s",str2);
if(strcmp(str2,str1)==0)
{
printf("Same\n");
}
else
{
printf("Not Same\n");
}
return 0;
}
Problem 3:Write a program to copy a string to another string. Suppose you are taking a string
from the user and storing that in S1[]. Then copy the inputted string to the S2[].
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("S1: ");
scanf(" %[^\n]s",str);
int len = strlen(str);
char cpy[len+1];
strcpy(cpy,str);
printf("S2: %s\n",cpy);
}
Problem 4: Write a program to concatenate two strings.
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str1[100];
char str2[100];
printf("String1: ");
scanf(" %[^\n]s",str1);
printf("String2: ");
scanf(" %[^\n]s",str2);
int len1=strlen(str1);
str1[len1]=' ';
for(int i = 0; i<=strlen(str2); i++)
{
str1[i+len1+1]=str2[i];
}
printf("%s\n",str1);
}
Problem 5: Write a program in C to count the number of letters and digits in a string.
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
int alpha=0;
int digits=0;
char str[100];
printf("String: ");
scanf(" %[^\n]s",str);
int len = strlen(str);
for(int i =0;i<len;i++)
{
if(str[i]>='0'&&str[i]<='9')
{
digits++;
}
else if(str[i]>='A'&&str[i]<='Z'||str[i]>='a'&&str[i]<='z')
{
alpha++;
}
}
printf("Letter: %d\nDigits: %d\n",alpha,digits);
return 0;
}
Problem 6: Write a program in C to count the number of vowels and consonants in a string.
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
int vowel=0;
int cons=0;
char str[100];
printf("String: ");
scanf(" %[^\n]s",str);
int len = strlen(str);
for(int i =0;i<len;i++)
{
if(str[i]>='A'&&str[i]<='Z'||str[i]>='a'&&str[i]<='z')
{
if(str[i]=='a'str[i]=='e'str[i]=='i'str[i]=='o'str[i]=='u'str[i]=='A'str[i]=='E'str[i]=='I'str[i]=='O'||str[i]
=='U')
{
vowel++;
}
else
{
cons++;
}
}
}
printf("Vowel= %d\nConsonant= %d\n",vowel,cons);
return 0;
}
Problem 7: Write a program to search a given character in string
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("String1: ");
scanf(" %[^\n]s",str);
const char* str2 = str;
char s;
printf("Character to be searched:");
scanf(" %c",&s);
char* result = strchr(str2,s);
if(result!=NULL)
{
printf("FOUND\n");
}
else
{
printf("NOT FOUND\n");
}
return 0;
}
Problem 8: Write a c program to find the position of the first occurrence of a given character in a
string.
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("String1: ");
scanf(" %[^\n]s",str);
const char* str2 = str;
char s;
printf("Character to be searched:");
scanf(" %c",&s);
char* result = strchr(str2,s);
if(result!=NULL)
{
printf("First occurrences of %c is at index %ld\n",s,result-str2);
}
else
{
printf("NOT FOUND\n");
}
return 0;
}
Problem 9: Write a c program all occurrences of a given character in a string.
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("String1: ");
scanf(" %[^\n]s",str);
const char* str2 = str;
char s;
printf("Character to be searched:");
scanf(" %c",&s);
int time=0;
while((str2 = strchr(str2,s))!=NULL)
{
str2++;
time++;
}
if(time>0)
{
printf("Character %c has been occurred %d times\n",s,time);
}
else
{
printf("NOT FOUND\n");
}
return 0;
}
Problem 10: Write a program to find occurrences of each character in a string.
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("String1: ");
scanf(" %[^\n]s",str);
int len = strlen(str);
int array[len];
for(int i =0;i<len;i++)
{
array[i]=0;
}
for(int i = 0; i<len; i++)
{
char* str2 = str;
char s = str[i];
if(array[i]==0)
{
int last=0;
int time =0;
while((str2 = strchr(str2,s))!=NULL)
{
last = str2-str;
str2++;
time++;
array[last]=-1;
}
printf("%c = %d ",s,time);
}
}
}
Problem 11: Write a c program to find whether a given string is palindrome or not(do not use
built-in functions).
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
scanf("%[^\n]s",str);
int array[26]={0};
int len = strlen(str);
for(int i =0; i<len; i++)
{
if(str[i]>='a'&&str[i]<='z')
{
char a=str[i];
array[a-'a']++;
}
else if(str[i]>='A'&&str[i]<='Z')
{
char a=str[i];
array[a-'A']++;
}
}
for(int i =0 ; i<len; i++)
{
if(str[i]>='a'&&str[i]<='z')
{
if(array[str[i]-'a']>0)
{
printf("%c %d ",str[i],array[str[i]-'a']);
array[str[i]-'a']=0;
}
}
else if (str[i]>='A'&&str[i]<='Z')
{
if(array[str[i]-'A']>0)
{
printf("%c occurs %d\n",str[i],array[str[i]-'A']);
array[str[i]-'A']=0;
}
}
}
}
Problem 12: Write a c program that will convert a lowercase string to an uppercase string.
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("String1: ");
scanf(" %[^\n]s",str);
int len = strlen(str);
for(int i =0;i<len;i++)
{
if(str[i]>='A'&&str[i]<='Z')
{
str[i]=str[i]+32;
}
else if(str[i]>='a'&&str[i]<='z')
{
str[i]=str[i]-32;
}
}
printf("%s\n",str);
}
Problem 13: Write a c program to find the number of words in a given string.
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("String1: ");
scanf(" %[^\n]s",str);
int len = strlen(str);
int word=0;
for(int i =0; i<len; i++)
{
if(str[i]==' '&&str[i-1]!=' ')
{
word++;
}
}
printf("%d\n",word+1);
}
Problem 14: Write a c program to find the number of words in a given string.
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char name[100];
printf("Name: ");
scanf(" %[^\n]s",name);
for(int i = 0;i<strlen(name);i++)
{
printf("%d ",name[i]);
}
printf("\n");
return 0;
}
Problem 15: Write a program to do the following: (a) To output the question “Who is the
inventor of C ?” (b) To accept an answer. (c) To print out “Good” and then stop, if the answer is
correct. (d) To output the message ‘try again’, if the answer is wrong. (e) To display the correct
answer when the answer is wrong even at the third attempt and stop
Source code:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
int r=3;
int c=0;
printf("Who is the inventor of C ?\n");
while(r)
{
}
}
if(!c)
printf("Answer is = Dennis MacAlistair Ritchie(Dennis Ritchie)\n");
}
Problem 16: Write a program to extract a portion of a character string and print the extracted
string. Assume that m characters are extracted, starting with the nth character.
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
int m;
int n;
printf("String:");
scanf(" %[^\n]s",str);
printf("m:");
scanf("%d",&m);
printf("n:");
scanf("%d",&n);
for(int i = n-1;i<(n+m)-1;i++)
{
printf("%c",str[i]);
}
printf("\n");
}
Problem 17: Write a program which will read a string and rewrite it in the alphabetical order. For
example, the word STRING should be written as GINRST
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
scanf(" %[^\n]s",str);
for(int i = 0; i<strlen(str); i++)
{
for(int j = 0; j<strlen(str)-1; j++)
{
if(str[j]>str[j+1])
{
char temp = str[j];
str[j]=str[j+1];
str[j+1]=temp;
}
}
}
printf("%s\n",str);
}
Problem 18: Write a program to replace a particular word by another word in a given string. For
example, the word “PASCAL” should be replaced by “C” in the text “It is good to program in
PASCAL language.”
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[50];
char target[10];
char replacement[10];
char nstr[100];
char store[50];
scanf("%[^\n]s",str);
fflush(stdin);
printf("Enter word to Replace:");
scanf("%[^\n]s",target);
fflush(stdin);
printf("Enter word to replace with:");
scanf("%[^\n]s",replacement);
fflush(stdin);
char strcopy[100];
strcpy(strcopy,str);
char *ptr = strstr(str,target);
if(ptr!=NULL)
{
long long int index = ptr-str;
str[index]='\0';
strcpy(nstr,str);
}
strcat(nstr,replacement);
char *ptr2 = strstr(strcopy,target);
if(ptr2!=NULL)
{
ptr2=ptr2+strlen(target);
}
strcat(nstr,ptr2);
printf("%s\n",nstr);
}
Problem 19: Write a program to replace a particular word by another word in a given string. For
example, the word “PASCAL” should be replaced by “C” in the text “It is good to program in
PASCAL language.”
Source code:
#include <stdio.h>
#include <string.h>
int main() {
record input[100];
int n;
printf("Enter price:\n-->>");
scanf("%d", &input[i].price);
}
printf("\nSearch\n");
char vehicle[100];
char start[10];
char end[10];
int count = 0;
printf("Matched records:\n\n");
for (int i = 0; i < n; i++) {
if (strcmp(input[i].vehicle, vehicle) == 0 && strcmp(input[i].month, start) >= 0 &&
strcmp(input[i].month, end) <= 0) {
printf("%s\n%s\n%d\n", input[i].vehicle, input[i].month, input[i].price);
count++;
}
}
if (!count) {
printf("No records found\n");
}
return 0;
}
Problem 20: Write a program that reads the cost of an item in the form TTTT.PP (Where TTTT
denotes Taka and PP denotes Paise) and converts the value to a string of words that expresses the
numeric value in words. For example, if we input 125.75, the output should be “ONE
HUNDRED TWENTY FIVE AND PAISE SEVENTY FIVE”
Source code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int whole=0,paise=0,hundred_part_whole=0,tenth_part=0,one_part=0;
char *hundreds[] = {"", "ONE HUNDRED", "TWO HUNDRED", "THREE HUNDRED",
"FOUR HUNDRED", "FIVE HUNDRED","SIX HUNDRED","SEVEN HUNDRED","EIGHT
HUNDRED","NINE HUNDRED"};
char *tens[] = {"", "", "TWENTY", "THIRTY", "FORTY",
"FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY" };
char *teens[] = {"", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN",
"FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTEEN","NINETEEN"};
char *ones[] = {"", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT",
"NINE"};
char number[10];
scanf(" %[^\n]s",number);
char wholepart[10];
char paisepart[10];
char* token = strtok(number,".");
if(token!=NULL)
{
strcpy(wholepart,token);
token=strtok(NULL,".");
strcpy(paisepart,token);
whole = atoi(wholepart);
hundred_part_whole=whole/100;
tenth_part=(whole%100)/10;
one_part=whole%10;
if(tenth_part==1)
{
printf("%s %s ", hundreds[hundred_part_whole],teens[one_part]);
}
else
{
printf("%s %s %s ", hundreds[hundred_part_whole], tens[tenth_part], ones[one_part]);
}
if(atoi(paisepart)>0)
{
int paise=atoi(paisepart);
int hundred_part_paise=paise/100;
int tenth_part_p=(paise%100)/10;
int one_part_p=paise%10;
if(tenth_part_p==1)
{
printf("AND %s %s\n", hundreds[hundred_part_paise],teens[one_part_p]);
}
else
{
printf("AND %s %s %s\n", hundreds[hundred_part_paise], tens[tenth_part_p],
ones[one_part_p]);
}
}
}
return 0;
}
Problem 21: . Develop a program that will read and store the details of a list of students in the
format. Roll No. Name Marks obtained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ………………. and produce the following output
list: (a) Alphabetical list of names, roll numbers and marks obtained. (b) List sorted on roll
numbers. (c) List sorted on marks (rank-wise list)
Source code:
#include<stdio.h>
void swap(char* one,char*two);
typedef struct student_form
{
int roll;
char name[20];
int marks;
} form;
int main()
{
form input[20];
int n;
printf("Enter the number of studets:\n-->>");
scanf("%d",&n);
for(int i =0; i<n; i++)
{
int c;
printf("Enter roll: ");
scanf("%d",&input[i].roll);
printf("Enter name: ");
scanf("%s",input[i].name);
printf("Enter marks: ");
scanf("%d",&input[i].marks);
}
for(int i =0; i<n; i++)
{
for(int j = i+1; j<n; j++)
{
if(strcmp(input[i].name,input[j].name)>0)
{
swap(input[i].name,input[j].name);
}
if(input[i].roll>input[j].roll)
{
int temp = input[i].roll;
input[i].roll=input[j].roll;
input[j].roll=temp;
}
if(input[i].marks>input[j].marks)
{
int temp2 = input[i].marks;
input[i].marks=input[j].marks;
input[j].marks=temp2;
}
}
}
printf("List of names in alphabatical order:\n");
for(int i =0; i<n; i++)
{
printf("%s\n",input[i].name);
}
printf("List of names in Roll order:\n");
for(int i =0; i<n; i++)
{
printf("%d\n",input[i].roll);
}
printf("List of names in Marks order:\n");
for(int i =0; i<n; i++)
{
printf("%d\n",input[i].marks);
}
}
void swap(char *one,char* two)
{ char temp[100];
strcpy(temp,one);
strcpy(one,two);
strcpy(two,temp);
}
Problem 22:Write a program to read a line of text from the keyboard and print out the number of
occurrences of a given substring
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[50];
char find[10];
printf("String: ");
scanf(" %[^\n]s",str);
printf("Substring: ");
scanf(" %[^\n]s",find);
int count=0;
char* ptr=str;
while((ptr=strstr(ptr,find))!=NULL)
{
count++;
ptr++;
}
printf("%d\n",count);
}
Problem 23 : Write a program that will copy m consecutive characters from a string s1 beginning
at position n into another string s2.
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str1[50];
char str2[50];
printf("String:");
scanf(" %[^\n]s",str1);
int size;
printf("m:");
scanf("%d",&size);
int index;
printf("n:");
scanf("%d",&index);
int t=0;
for(int i = index;i<index+size;i++,t++)
{
str2[t]=str1[i];
}
str2[t]='\0';
printf("%s\n",str2);
}
Problem 24 : Write a program to create a directory of students with roll numbers. The program
should display the roll number for a specified name and vice-versa.
Source code:
#include<stdio.h>
#include<string.h>
typedef struct studens
{
char name[10];
int ID;
} input;
input data[50];
int main()
{
printf("\n[1]Add data [2]Read Data\n");
int ch;
scanf("%d",&ch);
if(ch==1)
{
printf("Number of Entry:");
int n;
scanf("%d",&n);
for(int i =0; i<n; i++)
{
printf("Enter Student Name: ");
scanf(" %s",data[i].name);
printf("Enter Student Roll:");
scanf(" %d",&data[i].ID);
}
}
if(ch==2)
{
printf("\n[1] Search by roll [2] Search by name\n");
int ch2;
scanf("%d",&ch2);
if(ch2==1)
{
printf("Enter Roll:");
int id;
scanf("%d",&id);
for(int i = 0;i<50;i++)
{
if(id==(data[i].ID))
{
printf("\nStudent Name: %s\n\n",data[i].name);
break;
}
else if(i==49)
{
printf("Not found\n");
}
}
}
else if(ch2==2)
{
printf("Enter Name:");
char name[10];
scanf(" %s",name);
for(int i = 0;i<50;i++)
{
if(strcmp(name,data[i].name)==0)
{
printf("\nStudent ROLL: %d\n\n",data[i].ID);
break;
}
else if(i==49)
{
printf("Not found\n");
}
}
}
}
main();