[go: up one dir, main page]

0% found this document useful (0 votes)
17 views6 pages

String Uvt

The document contains a series of C programming exercises focused on string manipulation. It includes programs to reverse a string, count words, reverse the order of words, compare strings, sort a string, and find the occurrences of a substring. Each exercise is accompanied by example inputs and expected outputs, along with the corresponding C code.

Uploaded by

amorrihouwayda99
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)
17 views6 pages

String Uvt

The document contains a series of C programming exercises focused on string manipulation. It includes programs to reverse a string, count words, reverse the order of words, compare strings, sort a string, and find the occurrences of a substring. Each exercise is accompanied by example inputs and expected outputs, along with the corresponding C code.

Uploaded by

amorrihouwayda99
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
You are on page 1/ 6

TAYSSIR ABIDI

STRINGS

Exercise VI.1
Write a program in C to print a string in reverse order.
Example:
Input the string: Exercise
Expected output: esicrexE

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

void main(){
char string[100];
char string1[100]="";
int i;
printf("Input the string:\t");
gets(string);
for(i=strlen(string)-1;i>=0;i--){
char a[2]={string[i],'\0'};
strcat(string1,a);
}
puts(string1);
}
Exercise VI.2
Write a program in C to count the total number of words in a string.
Example:
Input the string: This is a string
Expected output: Total number of words is: 4

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

void main(){
char string[1000];
int i,count;
printf("Input the string:\t");
gets(string);
strcat(string," ");
count=0;
for(i=0;i<strlen(string);i++){
if(string[i]==' ' && string[i+1]!=' '){
count++;
}
}
printf("Total number of words is:\t%d",count);
}

2
Exercise VI.3
Write a C program to reverse order of words in a given string.
Example:
Input the string: I love learning programming
Expected output: programming learning love I

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

void main(){
char string[1000];
char string1[1000]="";
printf("Input the string:\t");
gets(string);
int i,j;
for(i=strlen(string)-1;i>=0;i--){
char word[100]="";
while(string[i]!=' ' && i>=0){
char a[2]={string[i],'\0'};
strcat(word,a);
i--;
}
for(j=strlen(word)-1;j>=0;j--){
char b[2]={word[j],'\0'};
strcat(string1,b);
}
strcat(string1," ");
}
puts(string1);
}

3
Exercise VI.4
Write a C program to compare two strings without using string library functions.
Example:
Input the 1st string: This is first string
Input the 2nd string: This is first string
Expected Output: Both strings are equal.

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

void main(){
char string1[1000];
char string2[1000];
int i,sum;
printf("Input the 1st string:\t");
gets(string1);
printf("Input the 2nd string:\t");
gets(string2);
sum=0;
for(i=0;i<strlen(string1);i++){
if(string1[i]!=string2[i]){
printf("Both strings are not equal.");
break;
}
else{
sum++;
}
}
if(sum==strlen(string1)){
printf("Both strings are equal.");
}
}
4
Exercise VI.5
Write a C program and an algorithm to sort a string in ascending order.
Example:
Input the string: string
Expected output: ginrst

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

void main(){
char string[100];
int i,j;
char a;
printf("Input the string:\t");
gets(string);
for(i=0;i<strlen(string);i++){
for(j=i+1;j<strlen(string);j++){
if(string[i]>string[j]){
a=string[i];
string[i]=string[j];
string[j]=a;
}
}
}
puts(string);
}

5
Exercise VI.6
Write a program in C to find the number of times a substring appears in a given string.
Example:
Input the original string: this is original string
Input the string to be searched for: str
Expected Output:
The string 'str' occurs 1 times

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

void main(){
char sentence[1000];
char word[100];
int i, j, count, sum;
printf("Input the original string:\t");
gets(sentence);
printf("Input the string to be searched for:\t");
gets(word);
count=0;
for(i=0;i<strlen(sentence);i++){
if(sentence[i]==word[0]){
sum=0;
for(j=0;j<strlen(word);j++){
if(sentence[i]==word[j]){
sum++;
i++;
}
}
i--;
if(sum==strlen(word)){
count++;
}
}
}
printf("The string '%s' occurs %d times", word, count);
}

You might also like