Home > Net >  C: Count how many letters are in the string and print the word reversed using functions
C: Count how many letters are in the string and print the word reversed using functions

Time:10-11

So I have managed to find whether a string is palindrome or not, But I can't seem to find how many letters are in the word given by the user and the word printed backward. Also is there a way I can replace gets(), because when I run the program it gives me that it is unsafe?

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


int isPalindrome(char *str) {
    int l,comp=0,n;

    n=strlen(str);
    for(l=0;l<n/2;l  ) {
        if(str[l]==str[n-l-1])
            comp  ;
    }

    if(comp==l)
        return 1;
    else
        return 0;
}

int main() {
    char str[1000];
    printf("Enter a word: ");
    scanf(str);

    if(isPalindrome(str))
        printf("The entered word is palindrome");
    else
        printf("The entered word is not palindrome");

    return 0;

}

CodePudding user response:

  • So I managed to reverse it in its own program, but how do I put the two programs together, or isn't that possible?
int string_length(char*);
void reverse(char*);
int main()
{
    char s[100];


    printf("Enter a string:");
    scanf("%s", s);
    reverse(s);
    printf("The reverse of the string is \"%s\".\n", s);
    return 0;
}
void reverse(char *s)
{
    int length, c;
    char *begin, *end, temp;


    length = string_length(s);
    begin  = s;
    end    = s;


    for (c = 0; c < length - 1; c  )
        end  ;


    for (c = 0; c < length/2; c  )
    {
        temp   = *end;
        *end   = *begin;
        *begin = temp;


        begin  ;

        end--;
    }
}
int string_length(char *pointer)
{
    int c = 0;


    while( *(pointer   c) != '\0' )
        c  ;


    return c;
}```

CodePudding user response:

So, I will answer your all questions sequentially;

  • Firstly, To count number of letters in word, you have already used a function strlen() in statement, n=strlen(str);. Here, n stores the number of letters in the given string.
    If you are not willing to use this function defined in <string.h>, you may keep inccrementing a variable intialized to zero starting from 1st letter till the end of the string:

    for(i=0;s[i]!='\0';i  );

    This will start from i=0 and keep on incrementing till you get s[i]='\0', i.e. NULL, and hence, now i contains the lenght of your string.

  • Now, to reverse string, you can use a pre-defined function strrev(), defined in <string.h>. This function takes string as parameter and returns reverse of the passed string. Use can use this function to print reverse string as :

    printf (" \n After the reverse of a string: %s ", strrev(str));

    If you are not willing to use library function, you can define function to reverse string as :

     for (i = 0; i < len/2; i  ) 
         {   
             temp = str1[i];  
             str1[i] = str1[len - i - 1];  
             str1[len - i - 1] = temp;  
         }  
    

    where, len contains the lenght of the given string, which could be obtained by any of the methods mentioned above.

    In this function, we are just swaping 1st letter with the last one. But only from 1st letter to the middle of string, beyond which, letters would be already swapped.

  • Finally to replace gets() function, you can use scanf() function, in which you will have to use "%s" as format specifier to get string from user. The need of gets() would arise when you need to take a string of multiple words as input. You could use scanf to take string of multiple words as input but you would have to modify your format specifier as:

scanf ( "%[ ^\n ]s", name ) ;   \\instead of scanf ( "%s", name ) ;

This will keep on taking string as inout until one hits Enter("\n").

  • Related