0

I'm trying to make a program that counts all the words that start and end with the same character. in C It tells me correctly which is the first and which is the last, I don't know how to make it show me the ones that are equal.

#include <stdio.h>
#include <string.h>

int main()
{
    char s[50];
    printf("Introdu propozitia : ");
    gets(s);
    int i, j = 0;

    
   // Traversing the Character array
    for (i = 0; i < strlen(s); i++) {

        // To store first character of
        // String if it is not a
        // whitespace.
        if (i == 0 && s[i] != ' ') {
            printf("%c ", s[i]);
        }
        
        if (s[i] == ' ')
            printf("%c", s[i -1]);
        
        // To check whether Character
        // is first character of
        // word and if yes store it.
        else if (i > 0 && s[i - 1] == ' ') {
            printf(" %c ", s[i]);
            
        }
        
        else if (i>0 && s[i] == ' ')
            printf("%c", s[i -1]);


        if(s[i]==s[i-1])
        Total ++;
        printf("\n Sunt : %d", Total);


        
    }
    
    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
z3r0
  • 1
  • 2
    _Side note:_ Never use `gets`--the man page says why. Change `i < strlen(s);` into `s[i] != 0;` – Craig Estey Nov 15 '22 at 17:38
  • Are you allowed to use `strtok`? – Craig Estey Nov 15 '22 at 17:48
  • @CraigEstey: There is no “the man page” for `gets`. There are multiple “a man page” pages, varying between both operating systems and release versions. Some of them do not say `gets` is deprecated. You do not know which one OP is using. – Eric Postpischil Nov 15 '22 at 17:58
  • Please read [Why is the `gets` function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/2505965) – Oka Nov 15 '22 at 18:02
  • @EricPostpischil I think you're being a bit nitpicky on this one. The linux man page certainly says so. And, so does freebsd, openbsd, and netbsd. Are there any others? ;-) Don't say "windows"--that OS is deprecated ;-) – Craig Estey Nov 15 '22 at 18:26
  • @CraigEstey: **You** have a man page on your system(s), which is(are) likely reasonably up to date. Not everybody in the world is so fortunate. We see some people coming to Stack Overflow with clues suggesting they are learning on old systems, and we do not know what backgrounds or environments they have or how hard it was for them to get as far as they have gotten so far. Telling somebody “the man page says why,” rather than actually saying why yourself, rebuffs them and suggests they have behaved deficiently in not checking it. **But you do not know their situation.** – Eric Postpischil Nov 15 '22 at 18:48

1 Answers1

0

For starters the function gets is unsafe and is not supported by the C Standard. Instead use either scanf or fgtes.

This if statement

if(s[i]==s[i-1])
        Total ++;

does not make sense.

Using the function strlen in the for loop is inefficient and redundant.

To find starts and ends of words in a string you can use standard string functions strspn and strcspn.

Here is a demonstration program.

#include <stdio.h>
#include <string.h>

int main( void )
{
    char s[50];
    printf( "Introdu propozitia : " );

    fgets( s, sizeof( s ), stdin );

    size_t count = 0;
    const char *delim = " \t\n";

    for ( const char *p = s; *p; )
    {
        p += strspn( p, delim );

        size_t n = strcspn( p, delim );

        if (n != 0 && p[0] == p[n - 1])
        {
            ++count;
        }

        p += n;
    }

    printf( "\nSunt : %zu\n", count );
}

Its output might look like

Introdu propozitia : 123454321 2345432 34543 454 5

Sunt : 5

If you want to output the words that satisfy the condition then add one statement in the if statement of the program

if (n != 0 && p[0] == p[n - 1])
{
    ++count;
    printf( "%.*s\n", ( int )n, p );
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335