2013-10-15 25 views
-1

我想返回一個句子,一個單詞全部以大寫形式返回。 我知道我想怎樣去做所有的事情,但我無法完成它的工作。 這是迄今爲止的代碼,但需要更多的工作。使字符串中的一個單詞出現在大寫,C編程

任何想法我現在很失落

這就是問題的措辭。

/* 
    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(燒焦SZ [],炭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') */ 
+2

爲什麼兩次'getline'? – haccks

+4

_「不能完全得到它的工作」_是必要的,但沒有足夠的條件對SO帖子 – P0W

+0

如何多一點信息?這看起來像是一個家庭作業問題,如果你能用文字描述你想做什麼,我們可以幫助你到達那裏。 – plinth

回答

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()

相關問題