#Not able to write program completely

23 messages · Page 1 of 1 (latest)

radiant violet
#

Have to reverse non-identifiers. identifiers: words that have characters from the set of "A"-"Z", "a"-"z", "0"-"9" or "_" (or so it is in my problem's case). it should also keep white spaces and new lines as in input.

my idea was to check string in advance and see if it contained anything beyond the identifier set, if yes then i would reverse the word.

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

int main()
{
    int i, j, n;
    char c;
    char word[100000];

    while(1)
    {
        word[0]='\0';
        scanf("%s%c", word,c);

        n=strlen(word); /* n is length of word */
        if(n == 0)
        {
            break;
        }
        else if(c == '\n') printf("\n");
        else if(c == ' ') printf(" ");
        else
        {
            for(j = 0; j < n; j++) /* thought of checking every character in string */
            {
                if( (string[j] >= '!' && string[j] <= '/') ||
                    (string[j] >= ':' && string[j] <= '@') ||
                    (string[j] >= '[' && string[j] <= '`') ||
                    (string[j] >= '{' && string[j] <= '~') ) /* if any character of string is from following set, reverse word
                    
                loop for reversing word... prob need it outside of for(j = 0; j < n; j++) loop
                for(i = n; i > 0; i--) printf("%c", word[i]); */
            }
        }
    }

return 0;
}
sharp wolfBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

indigo geodeBOT
#
Compiler Output
<source>: In function 'main':
<source>:20:40: error: expected ';' before 'else'
   20 |         else if(c == '\n') printf("\n")
      |                                        ^
      |                                        ;
   21 |         else if(c == ' ') printf(" ")
      |         ~~~~                            
Build failed
untold storm
#

just see if the first character is a letter or _

#

and then add to an arr or slice based on the amount of succeeding letters that are 0-9, or a letter or _

#
#include <stdio.h>
#include <ctype.h>
int main() {
  char str[] = " hello ";
  unsigned int idx = 0;
  unsigned int len = 1;
  unsigned int start = 0;
  while (str[idx++]) {
    if(!isalpha(str[idx]) && str[idx] != '_') continue;
    start = idx;
    while(isalnum(str[idx] || str[idx++] == '_')) len++;
    break;
  }
  printf("Returned str: \"%.*s\"", len, str[idx]);
}
indigo geodeBOT
#
Compiler Output
<source>:3:1: warning: return type defaults to 'int' [-Wimplicit-int]
    3 | main() {
      | ^~~~
<source>: In function 'main':
<source>:5:3: error: expected ',' or ';' before 'unsigned'
    5 |   unsigned int idx = 0;
      |   ^~~~~~~~
<source>:8:14: error: 'idx' undeclared (first use in this function)
    8 |   while (str[idx++]) {
      |              ^~~
<source>:8:14: note: each undeclared identifier is reported only once for each function it appears in
<source>:9:46: error: expected statement before ')' token
    9 |     if(!isalpha(str[idx]) && str[idx] != '_')) continue;
      |                                              ^
<source>:11:48: error: expected ')' before 'len'
   11 |     while(isalnum(str[idx] || str[idx] == '_') len++;
      |          ~                                     ^~~
<source>:13:3: error: expected expression before '}' token
   13 |   }
      |   ^
Build failed
untold storm
#

darn one semicolon

indigo geodeBOT
#
Compiler Output
<source>:3:1: warning: return type defaults to 'int' [-Wimplicit-int]
    3 | main() {
      | ^~~~
<source>: In function 'main':
<source>:9:46: error: expected statement before ')' token
    9 |     if(!isalpha(str[idx]) && str[idx] != '_')) continue;
      |                                              ^
<source>:11:48: error: expected ')' before 'len'
   11 |     while(isalnum(str[idx] || str[idx] == '_') len++;
      |          ~                                     ^~~
<source>:13:3: error: expected expression before '}' token
   13 |   }
      |   ^
Build failed
indigo geodeBOT
#
Program Output
Returned str: ""
Compiler Output
<source>:3:1: warning: return type defaults to 'int' [-Wimplicit-int]
    3 | main() {
      | ^~~~
indigo geodeBOT
#
Compilation successful

No output.

untold storm
#

@radiant violet Sorry, was having a rough day. Here's the completed example with comments:

#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
int main() {
  char str[] = " hello ";
  unsigned int idx = 0;
  unsigned int len = 1;
  unsigned int start = 0;
  bool init = 0;
  while (str[idx]) {
    if(!init) { // If there's no index, check if the current char could match the beginning of the identifier
      if(!isalpha(str[idx]) && str[idx] != '_') { idx ++; continue; } // We skip the entry if there is no string start char here
      start = idx;// If it does, initiate the string by setting idx and init;
      init = true;
    }
    else if(isalnum(str[idx]) || str[idx] == '_') len ++; // If we know that there was an indentifier in the prev index, we see if it continues here.
    else break; // After init is true, but we don't see any identifier chars, we just exit the loop
    idx ++;
  }
  printf("Returned str: \"%.*s\"", len, str + start);
}
#

I employed a state machine technique to match your identifier

radiant violet
#

Okay thanks! I will check and try to understand everything

sharp wolfBOT
#

@radiant violet Has your question been resolved? If so, run !solved :)

radiant violet
#

!solved