0% found this document useful (0 votes)
6 views31 pages

Preparation - 4

The document contains multiple C programs that demonstrate various string manipulation techniques, including counting string length, copying, concatenating, comparing, reversing, changing case, finding occurrences of characters, and deleting or replacing characters. Each program includes input and output examples to illustrate its functionality. The document serves as a comprehensive guide for basic string operations in C programming.

Uploaded by

nethra05bv
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views31 pages

Preparation - 4

The document contains multiple C programs that demonstrate various string manipulation techniques, including counting string length, copying, concatenating, comparing, reversing, changing case, finding occurrences of characters, and deleting or replacing characters. Each program includes input and output examples to illustrate its functionality. The document serves as a comprehensive guide for basic string operations in C programming.

Uploaded by

nethra05bv
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//Count the len of the string or word.

Input : gelly
Output: 5
#include <stdio.h>
#define MAX_SIZE 100
#include <string.h>

int main() {
char str[MAX_SIZE];
scanf(" %[^\n]",str);
int len = strlen(str);
printf("Length of the string is %d",len);

return 0;
}

//C program to copy one string to another string


Input : love
Output: love

#include <stdio.h>
#define MAX_SIZE 100
int main()
{
char arr1[MAX_SIZE];
char arr2[MAX_SIZE];
int c=0,i;
fgets(arr1,MAX_SIZE,stdin);
for(i=0;arr1[i] !='\0' ; i++){
arr2[i] = arr1[i];
}
arr2[i] ='\0';
printf("%s", arr2);
}

//concatenate two strings


Input : hi + hello
Output: hihello

#include <stdio.h>
#include<string.h>
#define MAX_SIZE 100
int main()
{
char arr1[MAX_SIZE];
char arr2[MAX_SIZE];
char arr3[MAX_SIZE];
int c=0,i,j,k;
fgets(arr1,MAX_SIZE,stdin);
fgets(arr2,MAX_SIZE,stdin);
for(i =0 ; arr1[i] != '\0' ; i++){
arr3[i] = arr1[i];

}
for(j= i-1,k=0 ; arr2[j] != '\0',arr2[k] != '\0' ;j++,k++){
arr3[j] = arr2[k];
}
printf("%s", arr3);
// printf("%s",strcat(arr1,arr2));

}
/compare the string(correct way)

Input : gelly gelly


Output: 0
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
int main() {
char arr1[MAX_SIZE];
char arr2[MAX_SIZE];

fgets(arr1, MAX_SIZE, stdin);


fgets(arr2, MAX_SIZE, stdin);

if (arr1[strlen(arr1) - 1] == '\n')
arr1[strlen(arr1) - 1] = '\0';
if (arr2[strlen(arr2) - 1] == '\n')
arr2[strlen(arr2) - 1] = '\0';
printf("%d", strcmp(arr1, arr2));
return 0;
}
Another approach
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
int main() {
char arr1[MAX_SIZE];
char arr2[MAX_SIZE];
scanf(" %[^\n]",arr1);
scanf(" %[^\n]",arr2);
int res = strcmp(arr1,arr2);
printf("%d ",res);
return 0;
}

Reversing a String…​
Input : i am chellam
Output: chellam am i

#include <stdio.h>
#include <string.h>
int main()
{ char str[100];
scanf(" %[^\n]",str);
int len = strlen(str);
printf("%d\n",len);
int word_index[50];
int index=1;
word_index[0]=0;
char ch = ' ';
for(int i=0; i<len; i++){
​ if(str[i] == ch){
​ word_index[index]=i+1;
​ index++;
​ }
}
for(int i=index-1; i>=0; i--){
​ for(int j=word_index[i]; j<len; j++){
​ if(str[j] != ch)
​ printf("%c",str[j]);
​ else
​ break;
​ }
​ printf(" ");
}
}

//convert lower or upper


Input : gelly
Output: GELLY
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
#include <stdlib.h>

int main() {
char arr1[MAX_SIZE];
char arr2[MAX_SIZE];
fgets(arr1, MAX_SIZE, stdin);
fgets(arr2, MAX_SIZE, stdin);
for(int i=0 ;arr1[i] != '\0'; i++){
// printf("%c",tolower(arr1[i]));
printf("%c",toupper(arr1[i]));
}
}

//print string as togglecase


Input : GeLLy
Output: gEllY
#include <stdio.h>
#define MAX_SIZE 100
#include<stdlib.h>
#include<string.h>
int main()
{
char str1[MAX_SIZE];
scanf(" %[^\n]", str1);
int len = strlen(str1);
int word_index[MAX_SIZE];
word_index[0]= 0;
int index= 1;
char ch = ' ';
for( int i=0 ; i< len ; i++){
if(str1 [i] == ch){
word_index[index] = i+1;
index++;
}
}

for(int i= index-1; i>=0 ;i--){


for( int j=word_index[i] ; j<len ; j++){
if(str1[j] != ch){
printf("%c", str1[j]);
}
else
break;
}
if(i!=0)
printf(" ");
}

// Palindrome of a string
Input : MOM
Output: MOM ​
#include <stdio.h>
#include <string.h>

int main()
{
char str[100];
scanf(" %[^\n]",str);
int len = strlen(str);
char rev[100];
int index=0;
for(int i=len-1;i>=0; i--){
​ rev[index]=str[i];
​ index++;
}
int res = strcmp(str,rev);
if(res==0){
​ printf("Palindrome");
}
else{
​ printf("Not a Palindrome");
}
}

// Find first occurrence index of the character


Input : gelly l
Output: index:2
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
int main()
{
char str[MAX_SIZE];
fgets(str,MAX_SIZE,stdin);
char search = getchar();
int len = strlen(str);
int index=-1;
for(int i=0; i< len ; i++){
if(str [i] == search){
index=i;
break;
}
}
if( index == -1){
printf(" character not found");
}
else{
printf("%c found at %d", search,index);

}
}

// Find last occurrence index of the character


Input : gelly l
Output: index :3
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100

int main()
{
char str[MAX_SIZE];
fgets(str,MAX_SIZE,stdin);
char search = getchar();
int len = strlen(str);
int index=-1;
for(int i=len-1; i>=0; i--){
if(str [i] == search){
index=i;
break;
}
}
if( index == -1){
printf(" character not found");
}
else{
printf("%c found at %d", search,index);

}
}

// Find all occurrences of a char


Input : gelly l
Output: index: 2 3
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100

int main()
{
char str[MAX_SIZE];
fgets(str,MAX_SIZE,stdin);
char search = getchar();
int len = strlen(str);
int index=-1;
for(int i=0; i< len; i++){
if(str [i] == search){
index= i;
printf("%c found at %d", search,index);
printf("\n");

}
}
if( index == -1){
printf(" character not found");
}
}

//Count all occurrences


Input : gelly l
Output: 2
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100

int main()
{
char str[MAX_SIZE];
fgets(str,MAX_SIZE,stdin);
char search = getchar();
int len = strlen(str);
int c=0;
for(int i=0; i< len-1; i++){
if(str [i] == search){
c++;
}
}
printf("%d", c);
}

//find frequency
Input : gelly
Output: idhu ungaluke therui..ha ha haa
#include <stdio.h>
#include <stdlib.h>
int main() {
​ char arr[100];
​ int freq[100];
​ scanf(" %[^\n]",&arr);
​ int len = strlen(arr);
​ for(int i=0; i<len; i++){
​ freq[i]= -1;
​ }
​ for(int i=0; i<len; i++){
​ int count=1;
​ for(int j=i+1; j<len; j++){
​if(arr[i]==arr[j]){
​ count++;
​ freq[j]=0;
​}
​ }
​ if(freq[i]!=0){
​freq[i]=count;
​ }
​ }
​ for(int i=0 ; i<len ;i++){
​ printf("%d ",freq[i]);
​ }
​ printf("\n");
​ for(int i=0; i<len ;i++){
​ if(freq[i]!=0 && arr[i] != ' ')
​ printf("% c occured %d times \n",arr[i],freq[i]);
​ }
​ return 0;
}

//find high frequency


#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main() {
​ char arr[100];
​ int freq[100];
​ int i;
​ int index=0;
​ scanf(" %[^\n]",arr);
​ int len = strlen(arr);
​ for(int i=0; i<len; i++){
​ freq[i]= -1;
​ }
​ for( i=0; i<len; i++){
​ int count=1;
​ for(int j=i+1; j<len; j++){
​if(arr[i]==arr[j]){
​ count++;
​ freq[j]=0;
​}
​ }
​ if(freq[i]!=0){
​freq[i]=count;
​ }
​ }
​ for( i=0 ; i<len ;i++){
​ printf("%d ",freq[i]);
​ }
​ printf("\n");
​ for(i=0; i<len ;i++){
​ if(freq[i]!=0 && arr[i] != ' ')
​ printf("%c occured %d times \n",arr[i],freq[i]);
​ }
​ int max = freq[0];
​ for( i=0 ;i< len ; i++){
​ if(freq[i] > max){
​ max= freq[i];
​ index=i;
​ }
​ }
​ printf("%c occurs max times - %d", arr[index], max);
​ return 0;
}

//delete first occurrance


#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
scanf( " %[^\n]",str);
int len= strlen( str);
char letter;
scanf(" %c",&letter);
printf("%c\n",letter);
int index=-1;
for( int i =0 ; i< len ; i++){
if( str[i] == letter ){
index =i;
break;
}
}
if (index != -1){
for( int j =0 ; j< len ; j++){
if (index == j){
continue;
}
else
printf("%c",str[j]);
}

}
else
printf("letter not found");
}

// delete last occurrence

#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
scanf( " %[^\n]",str);
int len= strlen( str);
char letter;
scanf(" %c",&letter);
printf("%c\n",letter);
int index=-1;
for( int i =len-1 ; i>=0 ; i--){
if( str[i] == letter ){
index =i;
break;
}
}
if (index != -1){
for( int j =0 ; j <len ; j++){
if (index == j){
continue;
}
else
printf("%c",str[j]);
}

}
else
printf("letter not found");

//delete all occurrences of a character


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
char str[MAX_SIZE];
char word;
scanf(" %[^\n]", str);
scanf(" %c", &word);
int len = strlen(str);
int l;
for( int i=0 ;i<len ; i++){
if(str[i] == word){
int k = i;
for( int j=k; j < len-1 ; j++){
str[j] =str [j+1];

}
len--;
i--;

}
}
for( l=0 ; l< len ; l++){
printf("%c",str[l]);

}
}

//delete the duplicates


Input : gelly
Output: gely

#include <stdio.h>
#include <stdlib.h>
int main() {
​ char arr[100];
​ int freq[100];
​ scanf(" %[^\n]",&arr);
​ int len = strlen(arr);
​ for(int i=0; i<len; i++){
​ freq[i]= -1;
​ }
​ for(int i=0; i<len; i++){
​ int count=1;
​ for(int j=i+1; j<len; j++){
​if(arr[i]==arr[j]){
​ count++;
​ freq[j]=0;
​}
​ }
​ if(freq[i]!=0){
​freq[i]=count;
​ }
​ }
​ for(int i=0 ; i<len ;i++){
​ printf("%d ",freq[i]);
​ }
​ printf("\n");
​ for(int i=0; i<len ;i++){
​ if(freq[i] != 0 && arr[i] != ' ')
​ printf("%c", arr[i]);

​ }
​ return 0;
}

//replace the first occurrence


Input : [Link] . y (old - . ) (new - ! )
Output: g!ell.y
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[100];
char old;
char neww;
scanf(" %[^\n]",str);
int len = strlen(str);
scanf(" %c", &old);
scanf(" %c", &neww);
for( int i=0 ; i< len ; i++){
if( str [i] == old){
str[i] = neww;
break;
}
}
printf("%s",str);
}

//replace the last occurrence


Input : [Link] . y (old - . ) (new - ! )
Output: [Link]!y

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[100];
char old;
char neww;
scanf(" %[^\n]",str);
int len = strlen(str);
scanf(" %c", &old);
scanf(" %c", &neww);
for( int i=len-1 ; i>=0 ; i--){
if( str [i] == old){
str[i] = neww;
break;
}
}
printf("%s",str);
}
//replace the last occurrence
Input : [Link] . y (old - . ) (new - ! )
Output: g!ell!y

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[100];
char old;
char neww;
scanf(" %[^\n]",str);
int len = strlen(str);
scanf(" %c", &old);
scanf(" %c", &neww);
for( int i=0; i< len ; i++){
if( str [i] == old){
str[i] = neww;

}
}
printf("%s",str);
}

//Trim leading white space


Input : hiii
Output: hii
// Trim trailing white space
Input : hiii .
Output: hii
#include <stdio.h>
#define MAX_SIZE 100
#include <string.h>

int main()
{
char str[MAX_SIZE];
scanf(" %[^\n]",str);
int len= strlen(str);
int end = len-1;
while(str[end]==' '){
end--;
}
int start =0;
while(str[start]==' '){
start++;
}
printf("\"");
for(int i=start; i<=end; i++){
printf("%c",str[i]);
}
printf("\"");

// finding the all occurance of a word in a string


// find first and last found also

#include <stdio.h>
#define MAX_SIZE 100
#include <string.h>
int main()
{
char str1[MAX_SIZE];
scanf(" %[^\n]",str1);
char str2[MAX_SIZE];
scanf(" %[^\n]",str2);
int len = strlen(str1);
int len2 = strlen(str2);
str2[len2]='\0';
int word_index[MAX_SIZE];
word_index[0]=0;
int index=1;
for(int i=0; i<len; i++){
if(str1[i]==' '){
word_index[index]=i+1;
index++;
}
}
for(int i=0; i<index; i++){
​ printf("%d ",word_index[i]);
}
printf("\n");
int occurance_arr[MAX_SIZE];
int occurance_index=0;
for(int i=0; i<index; i++){
int start=word_index[i];
int flag=0;
int k=0;
int word_end=0;
for(int j=word_index[i]; j<start+len2; j++){
if(str1[j]!=str2[k]){
​ flag=1;
​ break;
}
if(j==(start+len2-1)){
if((str1[j+1]==' ')||(str1[j+1]=='\0')){
​ word_end=1;
}
}
k++;
}
if((flag==0)&&(word_end==1)){
occurance_arr[occurance_index]=word_index[i];
occurance_index++;
}
}
if(occurance_index==0){
​ printf("Word Not Found");
}
else{
for( int i=0 ; i< occurance_index ; i++){
​ printf("%d ", occurance_arr[i]);
}
}
}

/// Removal of first and last and all occurrence of a word in a string​

#include <stdio.h>
#define MAX_SIZE 100
#include <string.h>
int main()
{
char str1[MAX_SIZE];
scanf(" %[^\n]",str1);
char str2[MAX_SIZE];
scanf(" %[^\n]",str2);
int len = strlen(str1);
int len2 = strlen(str2);
str1[len]=' ';
str1[len+1]='\0';
str2[len2]='\0';
int word_index[MAX_SIZE];
word_index[0]=0;
int index=1;
for(int i=0; i<len; i++){
if(str1[i]==' '){
word_index[index]=i+1;
index++;
}
}
int occurance_arr[MAX_SIZE];
int occurance_index=0;
for(int i=0; i<index; i++){
int start=word_index[i];
int flag=0;
int k=0;
int word_end=0;
for(int j=word_index[i]; j<start+len2; j++){
if(str1[j]!=str2[k]){
​ flag=1;
​ break;
}
if(j==(start+len2-1)){
if((str1[j+1]==' ')||(str1[j+1]=='\0')){
​ word_end=1;
}
}
k++;
}
if((flag==0)&&(word_end==1)){
occurance_arr[occurance_index]=word_index[i];
occurance_index++;
}
}
for(int i=0; i<occurance_index; i++){
​ printf("%d ",occurance_arr[i]);
}
printf("\n");
if(occurance_index==0){
​ printf("Word Not Found");
}
else{
printf("\nRemoval of First Occurance:\n");
for(int i=0; i<len; i++){
​ if(i==occurance_arr[0]){
​ i=i+len2;
​ }
​ else{
​ printf("%c",str1[i]);
​ }
}
printf("\nRemoval of last Occurance:\n");
for(int i=0; i<len; i++){
​ if(i==occurance_arr[occurance_index-1]){
​ i=i+len2;
​ }
​ else{
​ printf("%c",str1[i]);
​ }
}
printf("\nRemoval of all Occurance:\n");
for(int i=0; i<len; i++){
​ int flag=0;
​ for(int j=0; j<occurance_index; j++){
​ if(i==occurance_arr[j]){
​ i=i+len2;
​ flag=1;
​ break;
​ }
​ }
​ if(flag==0)
​ printf("%c",str1[i]);
}
}
}
//To print lowercase in uppercase and viceversa
Input: HeLlo Iam
Output: hElLO iAM

#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
scanf(" %[^\n]",str);
int len=strlen(str);
for(int i=0; i<len; i++){
​ if(str[i] >= 'a' && str[i]<='z'){
​ printf("%c",toupper(str[i]));
​ }
​ else if(str[i] >= 'A' && str[i]<='Z'){
​ printf("%c",tolower(str[i]));
​ }
​ else{
​ printf("%c",str[i]);
​ }
}
}

/// Anagram
#include <stdio.h>
#define MAX_SIZE 100
#include <string.h>
int main()
{
char str1[MAX_SIZE];
scanf(" %[^\n]",str1);
int len1 = strlen(str1);
char str2[MAX_SIZE];
scanf(" %[^\n]",str2);
int len2 = strlen(str2);
if(len1!=len2){
​ printf("This is Not an Anagram");
}
else{
​ for(int i=0; i<len1; i++){
​ str1[i] = tolower(str1[i]);
​ str2[i] = tolower(str1[i]);
​ }
​ for(int i=0; i<len1; i++){
​ for(int j=i+1; j<len1; j++){
​ if(str1[i]>str1[j]){
​ char a = str1[i];
​ str1[i] = str1[j];
​ str1[j] = a;
​ }
​ if(str2[i]>str2[j]){
​ char a = str2[i];
​ str2[i] = str2[j];
​ str2[j] = a;
​ }
​ }
​ }
​ int res = strcmp(str1,str2);
​ if(res==0){
​ printf("Its a Anagram");
​ }
​ else{
​ printf("Not a Anagram");
​ }

}
}​

// removal of irrelevant spaces in a String…​

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
int main()
{
char str[MAX_SIZE];
scanf(" %[^\n]",str);
int len = strlen(str);
/// trimming and trailing the spaces
int start =0;
while(str[start]==' '){
​ start++;
}
int end = len-1;
while(str[end]==' '||str[end]=='\n'){
​ end--;
}
///print the string by removing extra spaces
printf("\"");
for(int i=start; i<=end; i++){
​ if(str[i]!=' '){
​ printf("%c",str[i]);
​ }
​ else{
​ printf(" ");
​ int temp = i;
​ while(str[temp]==' '){
​ temp++;
​ }
​ i=temp-1;
​ }
}
printf("\"");
}

You might also like