Closed. This question is
off-topic。它当前不接受答案。
想改善这个问题吗?
Update the question,因此它是
on-topic,用于堆栈溢出。
6年前关闭。
我想返回一个句子,其中一个单词全部以大写形式返回。
我有点知道我要如何处理整个事情,但我只是无法完全解决问题。
这是我到目前为止的代码,但是需要更多工作。
我现在很迷失的任何想法
这是问题的措辞。
/*
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') */
使用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()