Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

Palindromes A palindrome is a word or phrase that reads the same backw (2).docx

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige

Hier ansehen

1 von 3 Anzeige

Palindromes A palindrome is a word or phrase that reads the same backw (2).docx

Herunterladen, um offline zu lesen

Palindromes
A palindrome is a word or phrase that reads the same backwards and forwards, ignoring punctuation, spaces, and anything that isn’t a letter.
Examples of Palindromes
Madam, I’m Adam.
Kayak
Racecar
A man, a plan, a canal – Panama!
Able was I, ere I saw Elba.
C Program ONLY (Please not C++ or Java)
I need to write a program that does the following: *USING STRINGS TO PERFORM THE FOLLOWING*:
1. opens a text file named “Lab12input.txt” containing multiple lines of text (.txt file has been provided already just need code to open .txt file)
2. for each line, print out “Palindrome:” and then the line of text if it IS a palindrome, and print out “Not a palindrome:” and the line of text if it is NOT a palindrome.
Thanks!
Solution
int checkispallindrome(char* string)
{
int i;
int l = strlen(string);
for (i = 0; i < l / 2; i++)
{
if (string[i] != string[l - i - 1])
cout<<”not a palindrome”;
}
cout<<”it is a palindrome”;
}
int main()
{
FILE* f = fopen(\"myfile.txt\", \"r\");
char string[10];
while(fscanf(f, \"%s\", string) > 0)
{
checkispallindrome(string;
printf(\"%s\ \", string);
}
}
.

Palindromes
A palindrome is a word or phrase that reads the same backwards and forwards, ignoring punctuation, spaces, and anything that isn’t a letter.
Examples of Palindromes
Madam, I’m Adam.
Kayak
Racecar
A man, a plan, a canal – Panama!
Able was I, ere I saw Elba.
C Program ONLY (Please not C++ or Java)
I need to write a program that does the following: *USING STRINGS TO PERFORM THE FOLLOWING*:
1. opens a text file named “Lab12input.txt” containing multiple lines of text (.txt file has been provided already just need code to open .txt file)
2. for each line, print out “Palindrome:” and then the line of text if it IS a palindrome, and print out “Not a palindrome:” and the line of text if it is NOT a palindrome.
Thanks!
Solution
int checkispallindrome(char* string)
{
int i;
int l = strlen(string);
for (i = 0; i < l / 2; i++)
{
if (string[i] != string[l - i - 1])
cout<<”not a palindrome”;
}
cout<<”it is a palindrome”;
}
int main()
{
FILE* f = fopen(\"myfile.txt\", \"r\");
char string[10];
while(fscanf(f, \"%s\", string) > 0)
{
checkispallindrome(string;
printf(\"%s\ \", string);
}
}
.

Anzeige
Anzeige

Weitere Verwandte Inhalte

Weitere von lmark1 (20)

Anzeige

Palindromes A palindrome is a word or phrase that reads the same backw (2).docx

  1. 1. Palindromes A palindrome is a word or phrase that reads the same backwards and forwards, ignoring punctuation, spaces, and anything that isn’t a letter. Examples of Palindromes Madam, I’m Adam. Kayak Racecar A man, a plan, a canal – Panama! Able was I, ere I saw Elba. C Program ONLY (Please not C++ or Java) I need to write a program that does the following: *USING STRINGS TO PERFORM THE FOLLOWING*: 1. opens a text file named “Lab12input.txt― containing multiple lines of text (.txt file has been provided already just need code to open .txt file) 2. for each line, print out “Palindrome:― and then the line of text if it IS a palindrome, and print out “Not a palindrome:― and the line of text if it is NOT a palindrome. Thanks! Solution int checkispallindrome(char* string) {
  2. 2. int i; int l = strlen(string); for (i = 0; i < l / 2; i++) { if (string[i] != string[l - i - 1]) cout<<”not a palindrome”; } cout<<”it is a palindrome”; } int main() { FILE* f = fopen("myfile.txt", "r"); char string[10]; while(fscanf(f, "%s", string) > 0) { checkispallindrome(string; printf("%s ", string); } }

×