Write a program that reads a line of text (not exceeding 200 characters) stores it in a TXT variable and then displays:
- the length L of the string.
- the number of 'e' contained in the string.
- the whole sentence backwards, without changing the content of the variable TXT.
- the whole sentence backwards, after reversing the order of characters in TXT.
Example
here is a small sentence !
! ecnetnes llams a si ereh
Difficulty level
Video recording
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
#define SIZE 200
void main() {
char TXT[SIZE], aux;
int i, counte, length, j;
printf("Enter a line of text:\n");
// scanf("%s", TXT); // WRONG because it reads just one word
gets(TXT); // it reads a line till the return carriage
// length of the string
for (i = 0; TXT[i] != '\0'; i++)
;
// TXT[i]=='\0';
printf("length=%d\n", i);
length = i;
// number of occurrence of 'e'
counte = 0;
for (i = 0; TXT[i] != '\0'; i++)
if (TXT[i] == 'e') // WRONG if(TXT[i]=="e")
counte++;
printf("Number of occurrence of 'e'=%d\n", counte);
// print it backward
printf("\n\nPrinting the string in reverse order\n");
for (i = length - 1; i >= 0; i--)
printf("%c", TXT[i]);
//reverse the string
i = 0;
j = length - 1;
for (; i < j; i++, j--)
{
aux = TXT[i];
TXT[i] = TXT[j];
TXT[j] = aux;
}
printf("\n\nString in reverse order\n");
puts(TXT);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Symmetric array