-1

我想返回一个句子,一个单词全部以大写形式返回。我有点知道我想如何处理整个事情,但我就是无法让它发挥作用。这是我到目前为止的代码,但需要更多的工作。

我现在很迷茫的任何想法

这就是问题的措辞。

/*
   strhlt - Highlight substring

   Author - Your Name

   Limitations: The function will search for and highlight (capitalize) one instance
                of the szHigh string.  It will not highlight a possible 
                2nd instance

   Hint:        Perhaps the function strindex from K&R can be utilized
                to solve part of the problem?

   In/Out: sz - source string to be highlighted 'in place'
   In: szHigh - string to search for and highlight
   return:    - Returns 0 (false) if szHigh is not found in sz, otherwise -1 (true)


   Sample:

      char szTxt[] = "These are the best of times";
      i = strhlt(szTxt, "best");

      will return -1 (true) and the text in szText has changed to 
      "These are the BEST of times"

    */

int strhlt(char sz[], char szHigh[]);

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

#pragma warning(disable : 4996)

/* getline: get line into s, return length */
int getline (char s[], int lim)
{
   int c, i;

   i = 0;
   while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
      s[i++] = c;
   s[i] = '\0';
   return i;
}

/* getline: get line into s, return length */
int getline (char s[], int lim)
{
   int c, i;

   i = 0;
   while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
      s[i++] = c;
   s[i] = '\0';
   return i;
}

/* strindex: return index of t in s , -1 if none */
int strindex (char s[], char t[])
{
   int i, j, k;

   for (i=0; s[i] != '\0'; i++){
      for (j=i, k=0; t[k] != '\0' && s [j]==t[k]; j++, k++)
         ;
      if (k > 0 && t[k] == '\0')
         return i;
   }
   return -1;
}


// int strhlt(char sz[], char szHigh[]); //

main()
{ 

   char sz [60];

   strcpy

   getline (sz,60);
   getline (sz, 60);

   strhlt (sz, something); 
   getline ();


   printf("Please enter short phrase (60 chars max): ");
   printf("\n\nPlease enter a word to highlight (10 chars max): %s", sz);

   getchar();
   return 0;
}

/* (sz >= 'A' && sz >= 'Z' || sz >= 'a' && sz >= 'z') */
4

1 回答 1

1

使用toupper(). 它将传递给它的字符大写。

示例代码:

#include <stdio.h>
#include <ctype.h>
int main ()
{
  int i=0;
  char str[]="Test String.\n";
  char c;
  while (str[i]) 
  {
    c=str[i];
    putchar (toupper(c));
    i++;
  }
  return 0;
}

您问题的解决方案代码:

int main ()
{
  char str[] ="I am best for all my life";
  bool onceFound = false;
  char * pch;

  int i=0;
  pch = strtok (str," ");

  while (pch != NULL)
  {
    if(!strcmp(pch,"best") && onceFound==false)
    {
        onceFound = true; // check for not capitalizing another instance of the same word
        while(pch[i] != NULL)
        {
            printf ("%c",(char)toupper(pch[i]));
            i++;
        }
        printf(" ");
    }
    else
        printf("%s ",pch);

    pch = strtok (NULL, " ");
  }
  return 0;
}

欲了解更多信息toupper()putchar()

于 2013-10-15T18:37:49.223 回答