Create an Odd Positioned Array from Original Array and Deter
Create an Odd Positioned Array from Original Array and Determine If Palindrome:
Hello! I have a project that I am having some trouble with. We are suppose to take in a string from the user and then create a new string with only the odd array space characters. Then we are supposed to check to see whether the string is a palindrome or not (same front and backwards ex./ nurses run). I wrote most of the code but, I would like to know what I am doing wrong and how to create a working program from this. Thank you!
Challenge Description 1. Pass in a string as an argument 2. Write a function that returns the elements on odd positions in a character string. 3. Then write a function that tests whether a string is a palindrome. For example ./challenge 5.exe aboda would print \"aca\" then print that this (aca s a palindrome... Submit A tarball in the form username c5.tar.gz that has the following structure: Top directory named USERNAME C5 that contains only the following files: your C program file(s) (and header .h) files, if any) a Makefile that generates an executable called challenge 5.exe from your program and header files Everything must work on the Linux server. The following are some examples of things that will result in no credit for the assignment: Late submission or no submission (start early!) Tarball expansion fails Failure to tarball the directory with the correct name Missing program fileSolution
Hey ! In your code, there are a few things I tweaked.
There are some points like using strlen to compute the lenght of string instead of sizeof, since sizeof would give the size of the variable, strlen would count the length of string until it encounters a NULL character.
Also, you can not just assign two string using \'=\', for that you could use strcpy().
Also, your code to check palindrome was not working, i made it a bit simpler. do check it.
Please find below the New Working Code :) Enjoy !
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void str_odd_only(char str[]);
int is_palindrome(char str[]);
int main() {
int i;
char str[200];
printf(\"\ Please enter a string: \ \");
scanf(\"%s\",str);
str_odd_only(str);
printf(\"%s\ \",str);
if(is_palindrome(str)==0)
printf(\"String is a palindrome\");
else
printf(\"String is not a palindrome.\ \");
return 0;
}
void str_odd_only(char str[]) {
int j=0;
int i=0;
char odd[200];
while (str[i] != \'\\0\') {
if (i % 2 != 0)
{odd[j++] = str[i];}
i++;
}
odd[j] = \'\\0\';
strcpy(str,odd);
}
int is_palindrome(char str[])
{ int flag=0;
int i=0;
int length = strlen(str);
for(i=0;i < length ;i++){
if(str[i] != str[length-i-1]){
flag = 1;
break;
}
}
return flag;
}

