找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3530|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

fgets(); strlen(); strstr();綜合應(yīng)用

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:60076 發(fā)表于 2014-4-10 14:27 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
#include <stdio.h>
#include <string.h>
#include <ctype.h>    /* fgets() strlen() strstr() */
fgets() strlen() strstr()

int i,j;

int main()
{
char str1[100];
char str2[40];

printf("\nEnter the string to be searched (less than 100 characters):\n");
fgets(str1, sizeof(str1), stdin);

printf("\nEnter the string sought(less than 40 characters):\n");
fgets(str2, sizeof(str2), stdin);

/* overwrite the newline character in each string *///overwrite: 重寫
str1[strlen(str1) - 1] = '\0';                                                //用\0取代回車,構(gòu)成新數(shù)組(字符串)
str2[strlen(str2) - 1] = '\0';

printf("\nFirst sting entered:\n%s\n", str1);
printf("\nSecond sting entered:\n%s\n", str2);

/* Convert both strings to uppercase *///uppercase: n. 大寫字母
for(i = 0; (str1[i] = toupper(str1[i])); i++);
for(j = 0; (str2[j] = toupper(str2[j])); j++);

printf("\nThe second string %s found in the first",((strstr(str1,str2)==NULL)?"was not":"was"));

return 0;
}

psb(87).png (27.57 KB, 下載次數(shù): 231)

psb(87).png
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表