How to resolve the algorithm Determine if a string has all the same characters step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Determine if a string has all the same characters step by step in the C programming language

Table of Contents

Problem Statement

Given a character string   (which may be empty, or have a length of zero characters):

Use (at least) these seven test values   (strings):

Show all output here on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Determine if a string has all the same characters step by step in the C programming language

1-First example: This C program takes a string from the command line and checks if all the characters in the string are identical. Here's a detailed explanation of how the program works:

  • The main function is the entry point of the program. It takes two arguments: argc (the number of command-line arguments) and argv (an array of strings containing the command-line arguments).

  • The program first checks if the number of command-line arguments is greater than 2. If it is, it prints a usage message and returns 0.

  • If the number of command-line arguments is 1 or if the length of the first argument is 1, it prints the input string, its length, and a message indicating that all characters are identical.

  • If the number of command-line arguments is greater than 1 and the length of the first argument is greater than 1, it initializes a character variable reference with the first character of the first argument and an integer variable len with the length of the first argument.

  • It then iterates through the string and checks if each character is equal to the reference character. If a character is found that is not equal to the reference character, it prints the input string, its length, the first different character, the hexadecimal value of the first different character, and the position of the first different character.

  • If all characters in the string are identical, it prints the input string, its length, and a message indicating that all characters are identical.

**Usage of the program:

To use the program, pass a string as the first command-line argument. For example:

$ ./a.out "hello"

Output:

Input String: "hello"
Length: 5
First Different Character: 'l'(0x6c) at Position: 3

*****This program can be used to check if a string contains duplicate characters or if all the characters in a string are the same.

****2-Second Example:

  • The program starts by including the necessary headers.

  • The main function is the entry point of the program. Its arguments are the number of command-line arguments (argc) and an array of strings containing the command-line arguments (argv).

  • The program first checks if argc is greater than 2. If it is, it prints a usage message and returns 0.

  • If argc is 1 or if the length of the first argument is 1, it prints the input string, its length, and a message indicating that all characters are identical.

  • If argc is greater than 1 and the length of the first argument is greater than 1, it initializes a character variable reference with the first character of the first argument and an integer variable len with the length of the first argument.

  • It then iterates through the string and checks if each character is equal to the reference character. If a character is found that is not equal to the reference character, it prints the input string, its length, the first different character, the hexadecimal value of the first different character, and the position of the first different character.

  • If all characters in the string are identical, it prints the input string, its length, and a message indicating that all characters are identical.

  • The report_different_char function prints a report on the given string, including its length, the first different character (if any), and the position of the first different character (if any). This function can be used to test the program.

  • The find_different_char function finds the first character in the given string that is different from the preceding characters. This function is used by the report_different_char function to find the first different character in a string.

  • The program can be compiled using a C compiler (such as gcc or clang).

Source code in the c programming language

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

int main(int argc,char** argv)
{
    int i,len;
    char reference;
    
    if(argc>2){
        printf("Usage : %s <Test String>\n",argv[0]);
        return 0;
    }

    if(argc==1||strlen(argv[1])==1){
        printf("Input string : \"%s\"\nLength : %d\nAll characters are identical.\n",argc==1?"":argv[1],argc==1?0:(int)strlen(argv[1]));
        return 0;
    }

    reference = argv[1][0];
    len = strlen(argv[1]);

    for(i=1;i<len;i++){
        if(argv[1][i]!=reference){
            printf("Input string : \"%s\"\nLength : %d\nFirst different character : \"%c\"(0x%x) at position : %d\n",argv[1],len,argv[1][i],argv[1][i],i+1);
            return 0;
        }
    }

    printf("Input string : \"%s\"\nLength : %d\nAll characters are identical.\n",argv[1],len);

    return 0;

}


/**
 * An example for RossetaCode - an example of string operation with wchar_t and
 * with old 8-bit characters. Anyway, it seem that C is not very UTF-8 friendly,
 * thus C# or C++ may be a better choice.
 */

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

/*
 * The wide character version of the program is compiled if WIDE_CHAR is defined
 */
#define WIDE_CHAR

#ifdef WIDE_CHAR
#define CHAR wchar_t
#else
#define CHAR char
#endif

/**
 * Find a character different from the preceding characters in the given string.
 * 
 * @param s the given string, NULL terminated.
 * 
 * @return the pointer to the occurence of the different character 
 *         or a pointer to NULL if all characters in the string
 *         are exactly the same.
 * 
 * @notice This function return a pointer to NULL also for empty strings.
 *         Returning NULL-or-CHAR would not enable to compute the position
 *         of the non-matching character.
 * 
 * @warning This function compare characters (single-bytes, unicode etc.).
 *          Therefore this is not designed to compare bytes. The NULL character
 *          is always treated as the end-of-string marker, thus this function
 *          cannot be used to scan strings with NULL character inside string,
 *          for an example "aaa\0aaa\0\0".
 */
const CHAR* find_different_char(const CHAR* s)
{
    /* The code just below is almost the same regardles 
       char or wchar_t is used. */

    const CHAR c = *s;
    while (*s && c == *s)
    {
        s++;
    }
    return s;
}

/**
 * Apply find_different_char function to a given string and output the raport.
 * 
 * @param s the given NULL terminated string.
 */
void report_different_char(const CHAR* s)
{
#ifdef WIDE_CHAR
    wprintf(L"\n");
    wprintf(L"string: \"%s\"\n", s);
    wprintf(L"length: %d\n", wcslen(s));
    const CHAR* d = find_different_char(s);
    if (d)
    {
        /*
         * We have got the famous pointers arithmetics and we can compute
         * difference of pointers pointing to the same array.
         */
        wprintf(L"character '%wc' (%#x) at %d\n", *d, *d, (int)(d - s));
    }
    else
    {
        wprintf(L"all characters are the same\n");
    }
    wprintf(L"\n");
#else
    putchar('\n');
    printf("string: \"%s\"\n", s);
    printf("length: %d\n", strlen(s));
    const CHAR* d = find_different_char(s);
    if (d)
    { 
        /*
         * We have got the famous pointers arithmetics and we can compute
         * difference of pointers pointing to the same array.
         */
        printf("character '%c' (%#x) at %d\n", *d, *d, (int)(d - s));
    }
    else
    {
        printf("all characters are the same\n");
    }
    putchar('\n');
#endif
}

/* There is a wmain function as an entry point when argv[] points to wchar_t */

#ifdef WIDE_CHAR
int wmain(int argc, wchar_t* argv[])
#else
int main(int argc, char* argv[])
#endif
{
    if (argc < 2)
    {
        report_different_char(L"");
        report_different_char(L"   ");
        report_different_char(L"2");
        report_different_char(L"333");
        report_different_char(L".55");
        report_different_char(L"tttTTT");
        report_different_char(L"4444 444k");
    }
    else
    {
        report_different_char(argv[1]);
    }

    return EXIT_SUCCESS;
}


  

You may also check:How to resolve the algorithm Fivenum step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Text processing/2 step by step in the Scala programming language
You may also check:How to resolve the algorithm Non-decimal radices/Input step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Send an unknown method call step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Associative array/Merging step by step in the Mathematica / Wolfram Language programming language