A program that prints even and odd characters from a string

This is for homework.

I need to write a program that asks the user to enter a string, then my program would separate the even and odd values ​​from the entered string. Here is my program.

#include <stdio.h> #include <string.h> int main(void) { char *str[41]; char odd[21]; char even[21]; int i = 0; int j = 0; int k = 0; printf("Enter a string (40 characters maximum): "); scanf("%s", &str); while (&str[i] < 41) { if (i % 2 == 0) { odd[j++] = *str[i]; } else { even[k++] = *str[i]; } i++; } printf("The even string is:%s\n ", even); printf("The odd string is:%s\n ", odd); return 0; } 

When I try to compile my program, I get two warnings:

  • For my scanf I get "format '%s' expects arguments of type char but argument has 'char * (*)[41]" . I'm not sure what this means, but I guess this is due to array initialization.

  • In a while it gives a warning about a comparison between a pointer and an integer. I'm not sure what that means, and I thought it was legal in C to make this comparison.

When I compile the program, I get random characters for both even and odd lines.

Any help would be appreciated!

+5
source share
4 answers

this expression is incorrect:

 char *str[41]; 

you declare 41 uninitialized strings. Do you want to:

 char str[41]; 

then scanf("%40s" , str); , no & and limit input size (security)

then the loop (where your while (str[i]<41) is wrong, it probably ends right away, since the letters begin with 65 (the ascii code is for "A"). You wanted to test i against 41, but the str[i] test str[i] t27>, otherwise you will get all the garbage after the nul-term char in one of the odd lines or even if the line length is not 40 bytes)

 while (str[i]) { if (i % 2 == 0) { odd[j++] = str[i]; } else { even[k++] = str[i]; } i++; } 

if you want to use a pointer (assignment requirement), just define str as before:

 char str[41]; 

scan the input value on it as indicated above, then indicate on it:

 char *p = str; 

And now that you have pointed to the buffer pointer, if you want to use reverence instead of accessing the index, you can do:

 while (*p) { // test end of string termination if (i % 2 == 0) { // if ((p-str) % 2 == 0) { would allow to get rid of i odd[j++] = *p; } else { even[k++] = *p; } p++; i++; } 

(we have to increase i for an even / odd test, or we would have to test p-str parity)

aaaand the last classic mistake (thanks to the last minute comments), even and odd do not have zero completion, so the risk of getting garbage at the end when printing them you need:

 even[k] = odd[j] = '\0'; 

(as another answer , check the concept of even and odd, the expected result can be the other way around)

+7
source

There are several problems in the code:

  • You define an array of pointers char *str[41] , not a char array.
  • You should pass a scanf array instead of its address: when a function is passed, the array decays to a pointer to its first element.
  • You must limit the number of characters read by scanf .
  • You should iterate to the end of the line, and not over all elements of the array, especially with (&str[i] < 41) , which compares the address of the i th element with a value of 41 , which makes no sense. The end of the line is a null terminator that can be checked with (str[i] != '\0') .
  • You must read the characters from str using str[i] .
  • You must invalidate the even and odd arrays.

Here is a modified version:

 #include <stdio.h> int main(void) { char str[41]; char odd[21]; char even[21]; int i = 0; int j = 0; int k = 0; printf("Enter a string (40 characters maximum): "); if (scanf("%40s", str) != 1) return 1; while (str[i] != '\0') { if (i % 2 == 0) { odd[j++] = str[i]; } else { even[k++] = str[i]; } i++; } odd[j] = even[k] = '\0'; printf("The even string is: %s\n", even); printf("The odd string is: %s\n", odd); return 0; } 

Note that your interpretation of even and odd characters implies offsets based on 1, i.e. the first character is an odd character. This is inconsistent with C approach, where even characters are interpreted as having a uniform offset from the beginning of the line, starting at 0.

+3
source

here is your solution :)

 #include <stdio.h> #include <string.h> int main(void) { char str[41]; char odd[21]; char even[21]; int i = 0; int j = 0; int k = 0; printf("Enter a string (40 characters maximum): "); scanf("%s" , str); while (i < strlen(str)) { if (i % 2 == 0) { odd[j++] = str[i]; } else { even[k++] = str[i]; } i++; } odd[j] = '\0'; even[k] = '\0'; printf("The even string is:%s\n " , even); printf("The odd string is:%s\n " , odd); return 0; } 

solved the error in the declaration, the value of the scan line, the condition of the while loop and the assignment of the array element. :)

+2
source

Many answers to all are ready to point out source code problems.

Below are some ideas for reducing memory usage, since 2 arrays odd[], even[] not needed.

When the "even" characters appear, print them.
As "odd" characters appear, move them to the first part of the array.

Alternate printing: if the code "%.*s" , the array does not need to terminate the null character.

 #include <stdio.h> #include <string.h> int main(void) { char str[41]; printf("Enter a string (40 characters maximum): "); fflush(stdout); if (scanf("%40s", str) == 1) { int i; printf("The even string is:"); for (i = 0; str[i]; i++) { if (i % 2 == 0) { str[i / 2] = str[i]; // copy character to an earlier part of `str[]` } else { putchar(str[i]); } } printf("\n"); printf("The odd string is:%.*s\n ", (i + 1) / 2, str); } return 0; } 

or simply

 printf("The even string is:"); for (int i = 0; str[i]; i++) { if (i % 2 != 0) { putchar(str[i]); } } printf("\n"); printf("The odd string is:"); for (int i = 0; str[i]; i++) { if (i % 2 == 0) { putchar(str[i]); } } printf("\n"); 
+2
source

Source: https://habr.com/ru/post/1273329/


All Articles