For this assignment, we will move on to Chapter 8. In this chapter, we learn abo
ID: 3853841 • Letter: F
Question
For this assignment, we will move on to Chapter 8. In this chapter, we learn about using strings and string conversions in our coding. Obviously as you know, using and manipulating strings in any kind of code, whether it be command line coding or full on object oriented programming is a definite task. For this assignment, your program needs to ask the user to input a string or a sentence. When the user presses enter, the program will display the same sentence spelled backwards. That is all we really need to do. Fairly simple week this week. Now, for the extra credit. To get 5 points extra credit, do the above programming. At the end of your program though, program a check in the code to check to see if the sentence is a palindrome. This means that when you take the sentence and reverse it, it will spell the same thing. Tell me if the sentence is a palindrome or not. If you can successfully tell me that, I will give you 5 points extra credit. Since extra credit is just that...extra credit, I cannot provide help for the extra credit. I can provide help for the assignment. Good luck! Here is an example of the completed program without the extra credit. Enter a line of text: This is a test sentence for C Programming. This same sentence printed backwards is: gnimmargorP C rof ecnetnes tset a si sihT_Explanation / Answer
#include<stdio.h>
#include<string.h>
int main() {
char str[200], temp;
int i, j = 0;
printf(" Enter a line of text :");
gets(str);
i = 0;//first character index
j = strlen(str) - 1;//last character index
//swap the first character of string with last character,second character with second last and so on
while (i < j)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++; //increment from beginning
j--; //decrement from end
}
printf(" This same sentence printed backwards is %s", str);
return (0);
}
Output:
Enter a line of text : This is test sentence for C Programming
This same sentence printed backwards is
gnimmargorP C rof ecnetnes tset si sihT
Checking if the string is palindrome
#include<stdio.h>
#include<string.h>
int main() {
char str[200],str1[200],temp;
int i, j = 0;
printf(" Enter a line of text :");
gets(str);
strcpy(str1,str);
i = 0;//first character index
j = strlen(str1) - 1;//last character index
//swap the first character of string with last character,second character with second last and so on
while (i < j)
{
temp = str1[i];
str1[i] = str1[j];
str1[j] = temp;
i++; //increment from beginning
j--; //decrement from end
}
printf(" This same sentence printed backwards is %s", str1);
if(strcmp(str,str1) == 0)
printf(" The string is a palindrome");
else
printf(" The string is not a palindrome");
return (0);
}
Output:
Enter a line of text : sore was I ere I saw eros
This same sentence printed backwards is
sore was I ere I saw eros
The string is a palindrome
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.