Reputation: 143
Please help me with the part of the function. In the function I've created a for loop that is supposed to count the suitable letters in the beginning of each word in the string. For example, if i pass the string "Beautiful skies" into the function and char 's', the variable "count" is must be equal to 1 after the for loop, but it doesn't happen. What's the problem? I'm pretty newbie in coding, but this one looks for me the right one, but for some reason this for loop doesn't work as I expected (This is only a part of the whole function, but the problem is in the for loop, cause it always returns NULL, even when there are some suitable letters):
char** rearrange_string(char *str, char letter, int *size)
{
char *search, **array, upper_letter = toupper(letter), *shift;
int count = 0, i=0, j=0, counter;
for (search = str; *search != '\0'; search++) {
if (*search == ' ') {
continue;
}
if (*search != letter || *search != upper_letter) {
while (*search != ' ') {
search++;
if (*search == '\0') {
break;
}
}
continue;
}
if (*search == letter || *search == upper_letter) {
count++;
while (*search != ' ') {
search++;
if (*search == '\0') {
break;
}
}
}
}
if (count == 0) {
printf("There is no suitable data. Please reinput the string.");
return NULL;
}
Upvotes: 0
Views: 386
Reputation: 1063
What about a function like this?:
#include <unistd.h>
#include <stdio.h>
size_t count_char(char *str, char s)
{
size_t count = 0;
for (size_t i = 0; str[i]; i++)
if (str[i] == s)
count += 1;
return (count);
}
This code will simply return the number of character found in the string, like:
printf("%li\n", count_char("Beautiful skies", 's'));
will print:
1
Upvotes: -1
Reputation: 143
Thanks guys! The problem was in the first if-statement in the for-loop. I should to put && instead of ||. Thank you! If somebody wants to see the whole program, here is a whole code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
char** rearrange_string(char *str, char letter, int *size);
void main()
{
int size=0, i=0;
char *str, letter, **array;
printf("Enter the string and press ENTER to check: ");
gets(str);
printf("Enter the character for check (only lower chars!): ");
scanf("%c", &letter);
array = rearrange_string(str, letter, &size);
if (array == NULL)
{
printf("\nNo data found, please relaunch the program.");
return;
}
printf("The output string(s) is(are):\n");
for (i=0; i<size; i++)
{
printf("%s", array[i]);
printf("\n");
}
printf("size: %d", size);
for (i=0; i<size; i++)
{
free(array[i]);
}
free(array);
}
char** rearrange_string(char *str, char letter, int *size)
{
char *search, **array, upper_letter = toupper(letter), *shift;
int count = 0, i=0, j=0, counter;
for (search = str; *search != '\0'; search++)
{
if (*search == ' ')
{
continue;
}
if (*search != letter && *search != upper_letter)
{
while (*search != ' ')
{
search++;
if (*search == '\0')
{
break;
}
}
continue;
}
if (*search == letter || *search == upper_letter)
{
count++;
while (*search != ' ')
{
search++;
if (*search == '\0')
{
break;
}
}
}
}
if (count == 0)
{
printf("There is no suitable data. Please reinput the string.");
return NULL;
}
*size = count;
array = (char**)malloc(count*sizeof(char*));
for (search = str; *search != '\0'; search++)
{
if (*search == ' ')
{
continue;
}
if (*search != letter && *search != upper_letter)
{
while (*search != ' ')
{
search++;
if (*search == '\0')
{
break;
}
}
continue;
}
if (*search == letter || *search == upper_letter)
{
counter = 1;
j=0;
shift = search;
while (*search != ' ')
{
counter++;
search++;
if (*search == '\0')
{
break;
}
}
array[i] = (char*)malloc(counter*sizeof(char));
while (*shift != ' ')
{
*(*(array+i)+j) = *shift;
j++;
shift++;
if (*shift == '\0')
{
break;
}
}
*(*(array+i)+j) = '\0';
i++;
}
}
return array;
}
Upvotes: 0
Reputation: 87
When running with the string "Beautiful skies"
the program has a buffer overflow because search
is out of the original string. In the case *search == letter
you increment search
until *search==0
then search
is incremented again at the end of the loop, so it goes beyond the allocated buffer.
Here is the analysis which shows that:
https://taas.trust-in-soft.com/tsnippet/t/419a23ac
Upvotes: 0
Reputation: 1302
*search != letter || *search != upper_letter
is always true
if letter != upper_letter
change it to:
*search != letter && *search != upper_letter
Upvotes: 2