0% found this document useful (0 votes)
30 views

Lab String

The document contains 6 programming exercises involving string manipulation in C language: 1. The first exercise defines an algorithm to reverse a string by swapping the first and last characters, then the second and second last, and so on. 2. The second exercise defines a function to count the number of words in a string by checking for space characters. 3. The third exercise defines an algorithm to reverse the words in a given phrase by iterating through the phrase from back and swapping characters into a reverse string. 4. The fourth exercise defines a function to compare two strings and check if they are equal by iterating through and comparing characters. 5. The fifth exercise defines a function to sort the

Uploaded by

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

Lab String

The document contains 6 programming exercises involving string manipulation in C language: 1. The first exercise defines an algorithm to reverse a string by swapping the first and last characters, then the second and second last, and so on. 2. The second exercise defines a function to count the number of words in a string by checking for space characters. 3. The third exercise defines an algorithm to reverse the words in a given phrase by iterating through the phrase from back and swapping characters into a reverse string. 4. The fourth exercise defines a function to compare two strings and check if they are equal by iterating through and comparing characters. 5. The fifth exercise defines a function to sort the

Uploaded by

emnagrichi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Exercise VI.

algorithm inverse include<stdio.h>


var #include<string.h>
ch: string int main()
i,l:integer {
perm:character int i, j;
begin char s[100];
1-write("Enter a word") char s1[100];
2-read(ch) printf("enter a string \n");
3-l←length(ch)-1 gets(s);
4-for(i←0,i<l/2,i++) i=(strlen(s)-1);
perm←ch[i] j=0;
ch[i]←ch[l-i] while(i>=0 && j<strlen(s))
ch[l-i]←perm {
end for s1[j]=s[i];
5-write("The inverse of the
i--;
word you entered is:",ch);
j++;
end.
}
printf("%s", s1);

return 0;

}
Exercise VI.2

#include <stdio.h>
#include <string.h>

int main()
{
char s[100];
int i;
int count=0;
printf("Enter a string \n");
scanf("%[^\n]s",s);

for(i=0;s[i]!='\0';i++)
{
if(s[i]==' ')
{count++;}

}
printf("the number of words is : %d\n",count+1);

return 0;
}
Exercise VI.3
#include<stdio.h>
#include<string.h>
main()
{
char phrase[100], reverse[100];
int len, i, j, start, end;

printf("Enter any phrase: ");


gets(phrase);

len = strlen(phrase);
j = 0;

// Start checking of words from the end of phrase


start = len - 1;
end = len - 1;

while(start > 0)
{
// If a word is found
if(phrase[start] == ' ')
{
// Add the word to the reverse phrase
i = start + 1;
while(i <= end)
{
reverse[j] = phrase[i];

i++;
j++;
}
reverse[j++] = ' ';

end = start - 1;
}

start--;
}

// Finally add the last word


for(i=0; i<=end; i++)
{
reverse[j] = phrase[i];

Exercise VI.4
}
j++;

// Add NULL character at the end of reverse phrase


#include <stdio.h>
#include <string.h>

int main()
{
char s[100],s2[100];
int i,j,k,max;
printf("Enter a string \n");
scanf("%s",s);
printf("Enter a string s2 \n");
scanf("%s",s2);
if(strlen(s)==strlen(s2))
{
for(i=0; i<strlen(s); i++)
{ if(s[i]!=s2[i])
break;
}
if(i==strlen(s))
printf("both strings are equal");
else
printf("both string are not equal");
}
return 0;
}
Exercise VI.5

#include <stdio.h>
#include <string.h>

int main()
{
char s[100],a;
int i,j,k,max;
printf("Enter a string \n");
scanf("%s",s);

max=s[0];
for(i=0; i<strlen(s); i++)
{
for(j=0; j<strlen(s)-1; j++)
{

if(s[j]>s[j+1])
{ max=s[j];
s[j]=s[j+1];
s[j+1]=max;
}
}
}
printf("%s\n", s);

return 0;
}
Exercise VI.6

#include <stdio.h>
#include <string.h>
main()
{
char str[100], sub[100];
int count = 0, count1 = 0,i, j, l;
printf("\nEnter a string : ");
scanf("%[^\n]s", str);
printf("\nEnter a substring : ");
scanf(" %[^\n]s", sub);
for (i = 0; i < strlen(str);)
{
j = 0;
count = 0;
while ((str[i] == sub[j]))
{
count++;
i++;
j++;
}
if (count == strlen(sub))
{
count1++;
count = 0;
}
else
i++;
}
printf("%s occurs %d times", sub, count1);
}

You might also like